magiska metoder: Skillnad mellan sidversioner

Från Wiki.linux.se
Hoppa till navigering Hoppa till sök
(Skapade sidan med 'Magic Methods ¶ Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. Caution All methods names starting with __ are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior. The following method names are considered magical: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serializ...')
 
Ingen redigeringssammanfattning
Rad 1: Rad 1:
Magic Methods
== Klassabstraktion ==
Magic methods are special methods which override PHP's default's action when certain actions are performed on an object.
PHP har abstrakta klasser och metoder. Klasser som definieras som abstrakta kan inte instansieras, och alla klasser som innehåller minst en abstrakt metod måste också vara abstrakta. Metoder som definieras som abstrakta deklarerar endast metodens signatur; de kan inte definiera implementeringen.


Caution
När en klass ärver från en abstrakt klass måste alla metoder som är markerade som abstrakta i föräldrarklassens deklaration definieras av barnklassen och följa de vanliga reglerna för arv och signaturkompatibilitet.
All methods names starting with __ are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior.
 
The following method names are considered magical: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), __unserialize(), __toString(), __invoke(), __set_state(), __clone(), and __debugInfo().
 
Warning
All magic methods, with the exception of __construct(), __destruct(), and __clone(), must be declared as public, otherwise an E_WARNING is emitted. Prior to PHP 8.0.0, no diagnostic was emitted for the magic methods __sleep(), __wakeup(), __serialize(), __unserialize(), and __set_state().
 
Warning
If type declarations are used in the definition of a magic method, they must be identical to the signature described in this document. Otherwise, a fatal error is emitted. Prior to PHP 8.0.0, no diagnostic was emitted. However, __construct() and __destruct() must not declare a return type; otherwise a fatal error is emitted.
 
__sleep() and __wakeup() ¶
public __sleep(): array
public __wakeup(): void
serialize() checks if the class has a function with the magic name __sleep(). If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then null is serialized and E_NOTICE is issued.
 
Note:
 
It is not possible for __sleep() to return names of private properties in parent classes. Doing this will result in an E_NOTICE level error. Use __serialize() instead.
 
Note:
 
As of PHP 8.0.0, returning a value which is not an array from __sleep() generates a warning. Previously, it generated a notice.
 
The intended use of __sleep() is to commit pending data or perform similar cleanup tasks. Also, the function is useful if a very large object doesn't need to be saved completely.
 
Conversely, unserialize() checks for the presence of a function with the magic name __wakeup(). If present, this function can reconstruct any resources that the object may have.
 
The intended use of __wakeup() is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.
 
Example #1 Sleep and wakeup


=== Exempel #1 Abstrakt klassexempel ===
<pre>
<?php
<?php
class Connection
abstract class AbstractClass
{
{
     protected $link;
     // Tvinga den ärvande klassen att definiera denna metod
     private $dsn, $username, $password;
    abstract protected function getValue();
      
     abstract protected function prefixValue($prefix);
     public function __construct($dsn, $username, $password)
 
    {
     // Gemensam metod
         $this->dsn = $dsn;
     public function printOut() {
        $this->username = $username;
         print $this->getValue() . "\n";
        $this->password = $password;
        $this->connect();
     }
     }
   
}
    private function connect()
    {
        $this->link = new PDO($this->dsn, $this->username, $this->password);
    }
   
    public function __sleep()
    {
        return array('dsn', 'username', 'password');
    }
   
    public function __wakeup()
    {
        $this->connect();
    }
}?>
__serialize() and __unserialize() ¶
public __serialize(): array
public __unserialize(array $data): void
serialize() checks if the class has a function with the magic name __serialize(). If so, that function is executed prior to any serialization. It must construct and return an associative array of key/value pairs that represent the serialized form of the object. If no array is returned a TypeError will be thrown.


Note:
class ConcreteClass1 extends AbstractClass
 
If both __serialize() and __sleep() are defined in the same object, only __serialize() will be called. __sleep() will be ignored. If the object implements the Serializable interface, the interface's serialize() method will be ignored and __serialize() used instead.
 
The intended use of __serialize() is to define a serialization-friendly arbitrary representation of the object. Elements of the array may correspond to properties of the object but that is not required.
 
Conversely, unserialize() checks for the presence of a function with the magic name __unserialize(). If present, this function will be passed the restored array that was returned from __serialize(). It may then restore the properties of the object from that array as appropriate.
 
Note:
 
If both __unserialize() and __wakeup() are defined in the same object, only __unserialize() will be called. __wakeup() will be ignored.
 
Note:
 
This feature is available as of PHP 7.4.0.
 
Example #2 Serialize and unserialize
 
<?php
class Connection
{
{
     protected $link;
     protected function getValue() {
    private $dsn, $username, $password;
         return "ConcreteClass1";
 
    public function __construct($dsn, $username, $password)
    {
         $this->dsn = $dsn;
        $this->username = $username;
        $this->password = $password;
        $this->connect();
     }
     }


     private function connect()
     public function prefixValue($prefix) {
    {
         return "{$prefix}ConcreteClass1";
         $this->link = new PDO($this->dsn, $this->username, $this->password);
     }
     }
}


    public function __serialize(): array
class ConcreteClass2 extends AbstractClass
    {
        return [
          'dsn' => $this->dsn,
          'user' => $this->username,
          'pass' => $this->password,
        ];
    }
 
    public function __unserialize(array $data): void
    {
        $this->dsn = $data['dsn'];
        $this->username = $data['user'];
        $this->password = $data['pass'];
 
        $this->connect();
    }
}?>
__toString() ¶
public __toString(): string
The __toString() method allows a class to decide how it will react when it is treated like a string. For example, what echo $obj; will print.
 
Warning
As of PHP 8.0.0, the return value follows standard PHP type semantics, meaning it will be coerced into a string if possible and if strict typing is disabled.
 
A Stringable object will not be accepted by a string type declaration if strict typing is enabled. If such behaviour is wanted the type declaration must accept Stringable and string via a union type.
 
As of PHP 8.0.0, any class that contains a __toString() method will also implicitly implement the Stringable interface, and will thus pass type checks for that interface. Explicitly implementing the interface anyway is recommended.
 
In PHP 7.4, the returned value must be a string, otherwise an Error is thrown.
 
Prior to PHP 7.4.0, the returned value must be a string, otherwise a fatal E_RECOVERABLE_ERROR is emitted.
 
Warning
It was not possible to throw an exception from within a __toString() method prior to PHP 7.4.0. Doing so will result in a fatal error.
 
Example #3 Simple example
 
<?php
// Declare a simple class
class TestClass
{
{
    public $foo;
     public function getValue() {
 
         return "ConcreteClass2";
     public function __construct($foo)
    {
         $this->foo = $foo;
     }
     }


     public function __toString()
     public function prefixValue($prefix) {
    {
         return "{$prefix}ConcreteClass2";
         return $this->foo;
     }
     }
}
}


$class = new TestClass('Hello');
$class1 = new ConcreteClass1;
echo $class;
$class1->printOut();
?>
echo $class1->prefixValue('FOO_') ."\n";
The above example will output:
 
Hello
__invoke()
__invoke( ...$values): mixed
The __invoke() method is called when a script tries to call an object as a function.


Example #4 Using __invoke()
$class2 = new ConcreteClass2;
 
$class2->printOut();
<?php
echo $class2->prefixValue('FOO_') ."\n";
class CallableClass
{
    public function __invoke($x)
    {
        var_dump($x);
    }
}
$obj = new CallableClass;
$obj(5);
var_dump(is_callable($obj));
?>
?>
The above example will output:
</pre>
Utdata från exemplet ovan:


int(5)
<pre>
bool(true)
ConcreteClass1
Example #5 Using __invoke()
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2
</pre>


=== Exempel #2 Abstrakt klassexempel ===
<pre>
<?php
<?php
class Sort
abstract class AbstractClass
{
{
     private $key;
     // Vår abstrakta metod behöver bara definiera de nödvändiga argumenten
 
     abstract protected function prefixName($name);
    public function __construct(string $key)
    {
        $this->key = $key;
    }
 
     public function __invoke(array $a, array $b): int
    {
        return $a[$this->key] <=> $b[$this->key];
    }
}
}


$customers = [
class ConcreteClass extends AbstractClass
    ['id' => 1, 'first_name' => 'John', 'last_name' => 'Do'],
    ['id' => 3, 'first_name' => 'Alice', 'last_name' => 'Gustav'],
    ['id' => 2, 'first_name' => 'Bob', 'last_name' => 'Filipe']
];
 
// sort customers by first name
usort($customers, new Sort('first_name'));
print_r($customers);
 
// sort customers by last name
usort($customers, new Sort('last_name'));
print_r($customers);
?>
The above example will output:
 
Array
(
    [0] => Array
        (
            [id] => 3
            [first_name] => Alice
            [last_name] => Gustav
        )
 
    [1] => Array
        (
            [id] => 2
            [first_name] => Bob
            [last_name] => Filipe
        )
 
    [2] => Array
        (
            [id] => 1
            [first_name] => John
            [last_name] => Do
        )
 
)
Array
(
    [0] => Array
        (
            [id] => 1
            [first_name] => John
            [last_name] => Do
        )
 
    [1] => Array
        (
            [id] => 2
            [first_name] => Bob
            [last_name] => Filipe
        )
 
    [2] => Array
        (
            [id] => 3
            [first_name] => Alice
            [last_name] => Gustav
        )
 
)
__set_state() ¶
static __set_state(array $properties): object
This static method is called for classes exported by var_export().
 
The only parameter of this method is an array containing exported properties in the form ['property' => value, ...].
 
Example #6 Using __set_state()
 
<?php
 
class A
{
{
    public $var1;
    public $var2;


     public static function __set_state($an_array)
    // Vår barnklass kan definiera valfria argument som inte finns i föräldrarnas signatur
    {
     public function prefixName($name, $separator = ".") {
        $obj = new A;
        if ($name == "Pacman") {
         $obj->var1 = $an_array['var1'];
            $prefix = "Mr";
         $obj->var2 = $an_array['var2'];
         } elseif ($name == "Pacwoman") {
         return $obj;
            $prefix = "Mrs";
         } else {
            $prefix = "";
        }
         return "{$prefix}{$separator} {$name}";
     }
     }
}
}


$a = new A;
$class = new ConcreteClass;
$a->var1 = 5;
echo $class->prefixName("Pacman"), "\n";
$a->var2 = 'foo';
echo $class->prefixName("Pacwoman"), "\n";
 
$b = var_export($a, true);
var_dump($b);
eval('$c = ' . $b . ';');
var_dump($c);
?>
?>
The above example will output:
</pre>
 
Utdata från exemplet ovan:
string(60) "A::__set_state(array(
  'var1' => 5,
  'var2' => 'foo',
))"
object(A)#2 (2) {
  ["var1"]=>
  int(5)
  ["var2"]=>
  string(3) "foo"
}
Note: When exporting an object, var_export() does not check whether __set_state() is implemented by the object's class, so re-importing objects will result in an Error exception, if __set_state() is not implemented. Particularly, this affects some internal classes. It is the responsibility of the programmer to verify that only objects will be re-imported, whose class implements __set_state().
 
__debugInfo()
__debugInfo(): array
This method is called by var_dump() when dumping an object to get the properties that should be shown. If the method isn't defined on an object, then all public, protected and private properties will be shown.
 
Example #7 Using __debugInfo()
 
<?php
class C {
    private $prop;
 
    public function __construct($val) {
        $this->prop = $val;
    }


    public function __debugInfo() {
<pre>
        return [
Mr. Pacman
            'propSquared' => $this->prop ** 2,
Mrs. Pacwoman
        ];
</pre>
    }
}


var_dump(new C(42));
?>
The above example will output:


object(C)#1 (1) {
  ["propSquared"]=>
  int(1764)
}
= Sidslut =
= Sidslut =
Orginalhemsidan på Engelska : https://www.php.net/manual/en/language.oop5.magic.php
Orginalhemsidan på Engelska : https://www.php.net/manual/en/language.oop5.magic.php

Versionen från 18 augusti 2024 kl. 19.21

Klassabstraktion ¶

PHP har abstrakta klasser och metoder. Klasser som definieras som abstrakta kan inte instansieras, och alla klasser som innehåller minst en abstrakt metod måste också vara abstrakta. Metoder som definieras som abstrakta deklarerar endast metodens signatur; de kan inte definiera implementeringen.

När en klass ärver från en abstrakt klass måste alla metoder som är markerade som abstrakta i föräldrarklassens deklaration definieras av barnklassen och följa de vanliga reglerna för arv och signaturkompatibilitet.

Exempel #1 Abstrakt klassexempel

<?php
abstract class AbstractClass
{
    // Tvinga den ärvande klassen att definiera denna metod
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);

    // Gemensam metod
    public function printOut() {
        print $this->getValue() . "\n";
    }
}

class ConcreteClass1 extends AbstractClass
{
    protected function getValue() {
        return "ConcreteClass1";
    }

    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass1";
    }
}

class ConcreteClass2 extends AbstractClass
{
    public function getValue() {
        return "ConcreteClass2";
    }

    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass2";
    }
}

$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";

$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_') ."\n";
?>

Utdata från exemplet ovan:

ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2

Exempel #2 Abstrakt klassexempel

<?php
abstract class AbstractClass
{
    // Vår abstrakta metod behöver bara definiera de nödvändiga argumenten
    abstract protected function prefixName($name);
}

class ConcreteClass extends AbstractClass
{

    // Vår barnklass kan definiera valfria argument som inte finns i föräldrarnas signatur
    public function prefixName($name, $separator = ".") {
        if ($name == "Pacman") {
            $prefix = "Mr";
        } elseif ($name == "Pacwoman") {
            $prefix = "Mrs";
        } else {
            $prefix = "";
        }
        return "{$prefix}{$separator} {$name}";
    }
}

$class = new ConcreteClass;
echo $class->prefixName("Pacman"), "\n";
echo $class->prefixName("Pacwoman"), "\n";
?>

Utdata från exemplet ovan:

Mr. Pacman
Mrs. Pacwoman


Sidslut

Orginalhemsidan på Engelska : https://www.php.net/manual/en/language.oop5.magic.php
PHP
Språkreferens
Språkreferens#Klasser_och_Objekt