Magic Methods
In PHP, the magic methods are special methods reserved by PHP that allow you to add various skills to your class . These methods start with two hyphens (__) as standard and can only be used in classes. The table below shows what these methods are and what they are used for.
Magic Method | Explanation | |
void | __construct ( mixed ... $ args ) | The constructive method is commonly used to construct the necessary initial configurations and pass them to value sub-methods of a property. |
void | __destruct ( void ) | Contrary to constructive, the destructive method goes into effect after using the last object of a class . |
mixed | __call ( string $ method , array $ parameters ) | When a method that does not belong to a class is invoked, it is activated. |
mixed |
__callStatic ( string $ method , array $ parameters ) |
It is enabled when a static method that does not belong to a class is called. This method must be specified statically. |
mixed | __get ( string $ name ) | It is activated when a non- belonging attribute is called . |
void | __set ( string $ name , mixed $ value ) | A qualifier that does not belong to a class is committed when a value assignment is made. |
string | __toString ( void ) | When an object is converted to a circuit or echo , it is switched on. |
bool | __isset ( string $ name ) | When a class does not belong to a class, it is activated by isset () . |
void | __unset ( string $ name ) | It is enabled with the use of unset () on a class that does not belong to a class . |
array | __sleep ( void ) | It is enabled with the use of a class, eg serialize () . It puts a sequence of names of the class-specific attributes into a special outputable form with serialize () . |
void | __wakeup ( void ) | If a class is serialized () , then the serialized private data is re- cycled if it is unserialized () . |
mixed | __invoke ( mixed ... $ args ) | If a class belongs to an instance, it is used as a function. |
void | __clone ( void ) | If a clone belongs to a clone , for example clone , it enters the loop. |
array | __debugInfo ( void ) | If a class is used with var_dump () , for example , it goes into effect. |
# Construct
The constructive method is commonly used to construct the necessary initial configurations and pass them to value sub-methods of a property. It also provides the sending of a classname parameter while deriving a class instance. This method is a class instance before the use of all methods and attributes to be set up and first enters once-off operation.
mixed | $ Args ... | Sequential parameters. |
return | void |
<?php class Example
{
public function __construct($parameter)
{
echo $parameter . ' guys!';
}
}
$example = new Example('Hi');
Now let's give an example of using Vehicle :: and Automobile :: classes that we created earlier .
class Vehicle
{
protected $color = 'White';
public function __construct(string $color = NULL)
{
if( $color !== NULL )
{
$this->color = $color;
}
}
public function startEngine(array $connection)
{
return $connection;
}
public function setColor(string $color)
{
$this->color = $color;
}
public function getColor() : string
{
return $this->color;
}
}
<?php
require 'Vehicle.php';
require 'Automobile.php';
$automobile = new Automobile('Blue'); # Vehicle::__construct() yöntemine parametre gönderiliyor.
echo $automobile->getColor();
In the above example, notice how we send the parameter when doing class declaration with new.
# Destruct
this method, in contrast to the __construct () constructor, is enabled after using the most recently used attribute or method of a class instance. So an example is the last working method. It is usually used to terminate a process that is started with the __construct () constructor after the use of the instance.
void | ||
return | void |
<?php class Example
{
public function __construct($parameter)
{
echo $parameter . ' guys!<br>';
}
public function __destruct()
{
echo 'Goodbye!';
}
}
$example = new Example('Hi');
Goodbye!
# Call & CallStatic
When a method that does not belong to a class is invoked, it is activated. The purpose of such use is to provide a flexible method of use.
string | $ method | returns the name of this invoked method if a method that does not belong to the class is called. |
array | $ parameters | this method invokes a list of the array types of the parameters sent. |
return | mixed |
<?php class Example
{
public function __construct($parameter)
{
echo $parameter . ' guys!<br>';
}
public function __call($method, $parameters)
{
echo $method . ' ' . $parameters[0] . '<br>';
}
public function __destruct()
{
echo 'Goodbye!';
}
}
$example = new Example('Hi');
$example->thank('you!')
thank you!
Goodbye!
Note that in the code above, __call () is activated when the Example :: thank () method is called .
Now let's give an example of using it over the Vehicle :: class we created earlier .
class Vehicle
{
protected $color = 'White';
public function __construct(string $color = NULL)
{
if( $color !== NULL )
{
$this->color = $color;
}
}
public function __call($method, $parameters)
{
throw Exception('Invalid object call!');
}
public function startEngine(array $connection)
{
return $connection;
}
public function setColor(string $color)
{
$this->color = $color;
}
public function getColor() : string
{
return $this->color;
}
}
<?php
require 'Vehicle.php';
require 'Automobile.php';
$automobile = new Automobile('Blue');
echo $automobile->getName();
this method is called __call () method is not different from the static invocation of the method is to enter. This method must be declared as statically because it enters the circuit with a static call.
<?php class Example
{
public function __construct($parameter)
{
echo $parameter . ' guys!<br>';
}
public static function __callStatic($method, $parameters)
{
echo $method . ' ' . $parameters[0] . '<br>';
}
}
Example::thank('you!');
# Get
this method is like the __call () method. However, when a property is called instead of a method, it is activated.
string | $ name | Your name is called. |
return | mixed |
<?php class Example
{
public function __get($property)
{
echo $property . '<br>';
}
}
$example = new Example;
echo $example->thankYou;
Now let's give an example of using it over the Vehicle :: class we created earlier .
class Vehicle
{
protected $color = 'White';
public function __construct(string $color = NULL)
{
if( $color !== NULL )
{
$this->color = $color;
}
}
public function __call($method, $parameters)
{
throw Exception('Invalid object call!');
}
public function __get($property)
{
throw Exception('Invalid property call!');
}
public function startEngine(array $connection)
{
return $connection;
}
public function setColor(string $color)
{
$this->color = $color;
}
public function getColor() : string
{
return $this->color;
}
}
<?php
require 'Vehicle.php';
require 'Automobile.php';
$automobile = new Automobile('Blue');
echo $automobile->getName;
# Set
The use of this method is triggered when you try to assign an attribute that does not belong to the class that triggers the __get () method.
string | $ name | Your name is called. |
mixed | $ Value | Your value is worth it. |
return | mixed |
<?php class Example
{
public function __get($property)
{
echo $property . '<br>';
}
public function __set($property, $value)
{
echo $property . ':' . $value;
}
}
$example = new Example;
$example->thank = 'you';
# ToString
Enters when a class instance is converted to string or echoed.
void | ||
return | string |
<?php class Example
{
protected $getName = __CLASS__;
public function __toString()
{
return $this->getName . '<br>';
}
public function setName($name)
{
$this->getName = $name;
return $this;
}
}
$example = new Example;
echo $example;
# Bu yöntem $this ile sınıfın kendisini döndürdüğünden sınıfın örneği
# gibi kabul edilip __toString() yönteminin tetiklenmesine neden olur.
echo $example->setName('New Name');
New Name
# Isset & Unset
this method is enabled when a non-class attribute is checked with isset () .
string | $ name | Your name is called. |
return | mixed |
<?php class Example
{
public function __isset($property)
{
echo 'Undefined property:' . $property;
}
}
$example = new Example;
if( isset($example->getName) )
{
# Kodlarınız...
}
The use of this method is like __isset () . Trigger when a non-unique attribute is checked with unset () instead of isset () .
<?php class Example
{
public function __unset($property)
{
echo 'Undefined property:' . $property;
}
}
$example = new Example;
if( unset($example->getName) )
{
# Kodlarınız...
}
# Sleep
It is enabled with the use of a class, eg serialize () . It puts a sequence of names of the class-specific attributes into a special outputable form with serialize () .
void | ||
return | array |
<?php class Example
{
protected $name, $phone;
public function __construct($name, $phone)
{
$this->name = $name;
$this->phone = $phone;
}
public function __sleep()
{
return ['name', 'phone']; # Serileştirilecek nitelik isimleri.
}
}
$example = new Example('New Name', '1234');
echo serialize($example);
# Wakeup
If a class is serialized () , then the serialized private data is re- cycled if it is unserialized () . The __sleep () method of the class must be used to trigger this method .
void | ||
return | void |
<?php class Example
{
protected $name, $phone;
public function __construct($name, $phone)
{
$this->name = $name;
$this->phone = $phone;
}
public function __sleep()
{
return ['name', 'phone'];
}
public function __wakeup()
{
echo 'Wakeup!<br>';
}
}
$example = new Example('New Name', '1234');
$data = unserialize(serialize($example));
var_dump($data);
object (Example) # 2 ( 2 ) {[ "name": protected] => string ( 8 ) "New Name" [ "phone": protected] => string ( 4 ) "1234"}
# Invoke
If a class belongs to an instance, it is used as a function.
mixed | $ Parameters ... | For example the parameters sent. |
return | mixed |
<?php class Example
{
public function __invoke(...$parameters)
{
foreach( $parameters as $parameter )
{
echo $parameter . '<br>';
}
}
}
$example = new Example;
$example('Example', 1, true, NULL, 'Data');
1
true
NULL
Data
# Clone
If a clone belongs to a clone , for example clone , it enters the loop.
void | ||
return | void |
<?php class Example
{
public function __clone()
{
echo 'Instance cloned!';
}
}
$example = new Example;
$clone = clone $example;
# DebugInfo
If a class is used with var_dump () , for example , it goes into effect.
void | ||
return | array |
<?php class Example
{
public function __debugInfo()
{
return debug_backtrace(2);
}
}
$example = new Example;
var_dump($example);
{
[0] => array ( 3 )
{
[ "function"] => "__debugınfo"
[ "class"] => "Example"
[ "type"] => " -> "
}
[1] => array ( 3 )
{
[" file "] => " C: \ xampp \ htdocs \ example.php "
[" line "] => 0
[" function "] => "var_dump "
}
}