The following functions are special methods in php, they all begin with __
two scores. They are reserved magical functions in php
These magic methods are all inherently defined within PHP. Even if you don’t write the code for them yourself, they still exist and the PHP parser will use them or call them when needed.
__construct()
is a special method in Object-oriented PHP that is automatically called when a new object is created and it is a part of a collection of predefined
methods known as magic methods.
Most classes have a constructor, and usually one or more function parameters are used to set the internal state of the object.
a Constructor must begin with following name :-
__construct()
You will probably define constructors in PHP classes, when you need that some attributes (properties of that class) to be created on the fly. For example, if we need to
write a class for Users, this class will have properties like first name, last name and etc. We can decide which properties must be defined(used) when ever an object of that class is instantiated. If you still need to understand the idea behind this, see the example below.
<?php
class User
{
//Class proberties
protected $_firstName;
protected $_lastName;
//Constructor with two parameters
public function __construct($firstName,$lastName)
{
$this->_firstName = $firstName;
$this->_lastName = $lastName;
}
}
?>
In order to use the above written class, we need to create an instance of the class, in another words we will create an object of that user class(meaning one user). But we wanted that when ever a new user is created, firstName and lastName properties must also be created. Our __construct() Magic method tells it and it says when you want Fore example register a new user, these properties must be given, other wise you will get error (warning from PHP).
<?php
//creating one user from the class (user)
$user = new User(); //this won't work and error is triggered
?>
The above class didn't work, because, when creating new object of that use class, we have defined arguments in our constructor, which says. When ever you instantiate that class, creating a new object you must also provide the arguments provided by the class constructor
<?php
//creating one user from the class (user)
$user = new User('kamara', 'Bomar');
//Dump information about variable $user
var_dump($user);
?>
The above Example works after providing the arguments for the class constructro.object(User)[1] protected '_firstName' => string 'kamara' (length=6) protected '_lastName' => string 'Bomar' (length=5)
Destructor
in php is contrary to constructor and it is a method which is automatically called when an object is destroyed.
The main objective of
destroying an object is to free the resources (memory allocations, open files or sockets, database connections and etc.)
Most classes have a constructor, and usually one or more function parameters are used to set the internal state of the object.
a __Destructors must begin with following name :-
__destruct
<?php
class User
{
//Class proberties
protected $_firstName;
protected $_lastName;
//Constructor with two parameters
public function __construct($firstName,$lastName)
{
$this->_firstName = $firstName;
$this->_lastName = $lastName;
}
//calling a destructor
public function __destruct()
{
}
}
?>
__call() is php magic method, which is triggered (invoked automatically) whenever an undefined method is called on class instance. in PHP OOP class, if you call a method of a class that has not yet been defined(that still doesn't exist), the __call() of that class will be automatically called by the php instead. Take a look at the following Example.
<?php
class User
{
//Class proberties
protected $_firstName;
protected $_lastName;
//Constructor with two parameters
public function __construct($firstName,$lastName)
{
$this->_firstName = $firstName;
$this->_lastName = $lastName;
}
//Define the __call magic method
public function __call($name, $arguments)
{
echo "When you call an undefined method, i will answer you";
}
}
$user = new User('kamara', 'Bomar');
//call a to non existing method.
$user->register();
?>
Output: When you call an undefined method, i will answer you
public function __call($name, $arguments){}
, we will get this fatal error and the code stops executing, ( ! ) Fatal error: Call to undefined method User::register()
__callStatic is magic method available from PHP 5.3.0 and above is automatically called if you try to access or call to an undefined (non existing method static method ) static method on a class. It does pretty much the same job as the __call() but on static methods in classes. See the following Example. We will try to call (access) to non existing static method on our User class.
<?php
class User
{
//Class proberties
protected $_firstName;
protected $_lastName;
//Constructor with two parameters
public function __construct($firstName,$lastName)
{
$this->_firstName = $firstName;
$this->_lastName = $lastName;
}
public static function __callStatic($name, $arguments)
{
// value of $name is case sensitive,which is method name.
echo "you called to an undefined static method '$name',
but i will answer you instead";
}
}
$user = new User('Johnson', 'Jack');
//call a to non existing method.
$user::register();
?>
Output: you called to an undefined static method 'register', but i will answer you instead
public function __callStatic($name, $arguments){}
, we will get this fatal error and the code stops executing, ( ! ) Fatal error: Call to undefined method User::register()
__get()is invoked when client code attempts to read an undeclared property.In other words, when you try to read properties of class,
which are not yet defined (not existing) or if they are inaccessible, then the __get()
method is called automatically with
a single string argument containing the name of the property that the client is attempting to access. Whatever you
return from the __get()
method will be sent back to the client as if the target property exists with that value.
using the __clone() you can controll how PHP clones an object.
when a __clone() method is used in your class.
When this method exists, PHP allows __clone() to override its default behavior, as
shown:
which are not yet defined (not existing) or if they are inaccessible, then the __get()
method is called automatically with
a single string argument containing the name of the property that the client is attempting to access. Whatever you
return from the __get()
method will be sent back to the client as if the target property exists with that value.
Using the clone operator on objects stored in properties causes PHP to check whether
any of those objects contain a __clone() method. If one exists, PHP calls it. This repeats
for any objects that are nested even further.
This process correctly clones the entire object and demonstrates why it’s called a deep
copy.