PHP Magic Methods And Magic Constants 2023

By XiaoXin
A Bit Randomly

ExampleFile cannot be used with other modifiersThe type that file can modifyFile non-decorable typeFile can have one or more classes with the same nameSummarizeC# 11 adds a file-scoped type feature: a new file modifier tha... Read The Keyword `file` Of C#11 New Features

Main Contents
  1. Class method:
  2. Method overloading:
  3. Attribute overloading:
  4. Serialization related: 
  5. Manipulating classes and object methods:
  6. Constant:

PHP Magic Methods And Magic Constants

Class method:

1. __construct();

Description: A class with a constructor will call this method every time a new object is created, which is suitable for doing some initialization work before using the object. If a constructor is defined in a subclass, the constructor of its parent class will not be called implicitly. To execute the constructor of the parent class, you need to call parent::__construct() in the constructor of the child class. If the subclass does not define a constructor, it will be inherited from the parent class just like a normal class method.

2. __destruct();

Description: A destructor is executed when all references to an object are deleted or when the object is explicitly destroyed. 

Method overloading:

3. __call(); Description: When calling an inaccessible method in an object, the __call(); method will be called.
4. __callStatic(); Description: When calling an inaccessible method in a static way, the __callStatic(); method will be called.

Attribute overloading:

( only valid for private protected member attributes in the class )

5. __get(); Attribute overloading: ( only valid for private protected member attributes in the class )when reading the value of an inaccessible attribute . Description: __get() is called
6. __set();when assigning a value to an inaccessible attribute . Description: __set() will be called
7. __isset();is called on an inaccessible attribute isset() or empty() is called when Description: __isset() .
8. __unset();is called on an inaccessible attribute unset() is called when Description: __unset() .

Serialization related: 

9. __sleep() ; Description: Called during serialization, the serialize() function will check whether the magic method exists in the class. If present, this method will be called before performing the serialization operation.
10. __wakeup(); Description: unserialize() checks for the existence of a __wakeup() method. If it exists, this method will be called first and used in deserialization operations, such as re-establishing a database connection, or performing other initialization operations.

Manipulating classes and object methods:

11. __toString(); Description: The method is used to call when a class is treated as a string, such as outputting a class as a string
12. __invoke(); when attempting to invoke an object as a function . Description: The __invoke() method is automatically invoked
13. __ set_state(); Description: This static method is called when calling var_export() to export a class. The only parameter of this method is an array
14. __clone(); Description: When the copy is completed, if the __clone() method is defined, the __clone() method in the newly created object (the object generated by copying ) will be called, which can be used to modify the value of the attribute.
15. __autoload(); Description: This method can automatically instantiate the required classes. When the program wants to use a class but has not been instantiated, the method looks for a file with the same name as the class in the specified path. Otherwise report an error.
16 __debugInfo(); Description : The feature added in php5.6, var_dump() is triggered when a class is created, and returns an array containing object properties.


Explanation: PHP reserves all class methods starting with __ (two underscores) as magic methods. So when defining class methods, except for the magic methods mentioned above, it is recommended not to prefix them with __. You cannot use these method names when naming your own class methods, unless you want to use their magic functions.

Constant:

__LINK__ // The current line number in the file.
__FILE__ //The full path and filename of the file. If used in an included file, returns the name of the included file.
__DIR__ //The directory where the file is located. If used in an included file, returns the directory where the included file resides, equivalent to dirname(__FILE__).
__FUNCTION__ //Function name. Since PHP 5 this constant returns the name (case sensitive) of the function as it was defined. In PHP 4 the value is always lowercase.
__CLASS__ //The name of the class. Since PHP 5 this constant returns the name (case sensitive) of the class as it was defined. In PHP 4 the value is always lowercase.
__METHOD__ //The method name of the class (newly added in PHP 5.0.0). Returns the name (case-sensitive) of the method as it was defined.
__NAMESPACE__ //The name of the current namespace (case sensitive). This constant is defined at compile time (new in PHP 5.3.0).

Continuing...

Please Share This Article Thank You!

Parts of Top Latest PHP Interview Questions And Answers For Experienced
PHP get the IP address of the client

What's wrong with the IP address obtained with $_SERVER? $_SERVER['REMOTE_ADDR']; Get through the global array getenv('REMOTE_ADDR'); Get through environment variables When the client uses a proxy, the real IP address cann...

PHP function to obtain the content of the file and the corresponding

1: file_get_contents gets the content of the file (can be obtained by getting and posting), and the entire file is read into a string. 2: Open the URL with fopen, and get the content by getting (with the help of fgets() fu...