PHP magic functions

PHP magic functions

PHP magic functions are the methods that have some special names and starting with two underscores and will be triggered in response to particular PHP events.

These magic functions are as follows:

__construct

It is a Php magic functions that gets called when we create object of the class. Therefore, it is suitable for any initialization. It allows the developers to declare constructor methods of the classes.

Syntax:

void __construct ([ mixed $args = “” [, $… ]] )

Example:

PHP magic functions

__destruct

This magic method is used to destroy the object of the class. It is just opposite of the constructor.

Syntax:

void __destruct ( void )

Example:

magic functions

__call()

This magic class is used to triggered when invoking inaccessible methods in an object context.

Syntax:

public mixed __call ( string $name , array $arguments )

Example:

PHP magic function

__callStatic()

It is used to trigger when invoking inaccessible methods in a static context.

Syntax:

public static mixed __callStatic ( string $name , array $arguments )

Example:

magic function

__get()

It is used to utilize data reading from an inaccessible properties.

Syntax:

public mixed __get ( string $name )

Example:

Get magic function

__set()

It is used to write data to an inaccessible properties.

Syntax:

public void __set ( string $name , mixed $value )

Example:

magic function Set

__isset()

It is used to trigger by calling isset() or empty() on an inaccessible properties.

Syntax:

public bool __isset ( string $name )

Example:

isset functions

__unset()

It is used to invoke when unset() is used on inaccessible properties.

Syntax:

public void __unset ( string $name )

Example:

PHP magic functions

__sleep()

This method is used when the object is serialised and helps you to control what gets serialised. It is also used to commit pending data or perform similar cleanup tasks.

Syntax:

public array __sleep ( void )

Example:

magic function

__wakeup()

This method is just the opposite of the __sleep() method and allows us to re-establish any database connections during serialization that may have been lost and perform other reinitialization tasks.It also allows you to alter the behaviour of the unserialization of the object.

Syntax:

void __wakeup ( void )

Example:

PHP magic function

__toString()

This method allows a class to decide what will be the reaction when it is treated or behaved like a string. For example when it is echoed.

Syntax:

public string __toString ( void )

Example:

PHP magic function

__invoke()

This method is used when a script calls an object as a function.

Syntax:

mixed __invoke ([ $… ] )

Example:

Invoke

__set_state()

It is called in response to an instance of your object being passed to the var_export function where var_export returns a parsable string representation of a variable.

Syntax:

static object __set_state ( array $properties )

Example:

setstate

__clone()

It is used to make the copy of an object instead of having two variables pointing to the same actual data.

Syntax:

void __clone ( void )

Example:

clone

__debugInfo()

This method is used by var_dump() to get the properties while dumping an object that should be shown.

Syntax:

array __debugInfo ( void )

Example:

debuginfo