Common magic Methods in PHP summary _php tips

Source: Internet
Author: User

This article detailed PHP in the common magic methods are summarized, share for everyone to reference, the specific content as follows

1, PHP to all "__" as the beginning of the method as a magic method, so any custom method can not be "__" the beginning
The overload provided by PHP refers to the dynamic creation of a property or method. Overloading is accomplished by means of magic. The parameters of these magic methods are not drinkable for delivery, __get (), __set (), __isset (), __unset (), implementation class property overload.
2. The __get () method is invoked when accessing a property that is not accessible in the class
3. When accessing a property that is not accessible in a class, the __get (), __set (), __isset (), __unset (), and the Isset () are invoked when the property is accessed using the function empty (), or __isset (), depending on the situation. Method, the __unset () method is invoked when the unset () function is used.
4. These four methods cannot be defined with a static keyword, and after php5.3.0, a warning level warning will occur when the 4 methods are defined using the static method.
5. What is inaccessible: (1) properties that are not in the class, (2) Private variables are accessed outside the class. Method overloads, which are implemented by __call () and __callstatic (), and the difference is whether a context environment is a static method, which is invoked when an inaccessible static method is invoked. __callstatic () is added in php5.3.0.
6, __sleep () and __wakeup ()
When Serialize () is serialized, it checks for the existence of the __sleep () function and, if it exists, invokes __sleep (), returns an array containing all the variable names, and if the method does not return any content, the null is serialized and a notice level error is generated.
The __wakeup () method, instead of the __sleep (), is invoked when the Unserialize () is invoked
7, __tostring (), let a class determine how it converts to a string, before php5.2.0, this method can only be combined with the echo or the Print method, php5.2.0, can be combined with printf () decoration, but the modification is not a similar% A non-character modifier such as d.
8, __invoke (), when trying to invoke the function to call the object, it will start this function, only after php5.3 effective.
9, __set_state () static method, when the call Var_export (), will call this static method, php5.1.0 after the entry into force. The only parameter is to receive an array.
10, __construct () and __destruct (), constructors and destructors, constructors are primarily used for class initialization, destructors are run when all references to an object are deleted, or when an object is displayed for destruction. Throwing an exception in a destructor is not allowed, otherwise a fatal error is set off, and the destructor is invoked at the end of the script, at which point all HTTP headers have been emitted.
11, __clone (), object replication, this method can not be called directly, must be shown by the Clone keyword call

Code:

When an object is instantiated, the __construct method of the object is invoked $obj = new Object ();
When assigning values to a property that does not exist for an object, the __set method of the object is invoked $obj->title = "Hello";
When you use a property that does not exist for an object, the __get method of the object is invoked echo $obj->title;
When a method that does not exist for an object is invoked, the __call method of the object is invoked $obj->test ("Hello", 123,567);
When calling a nonexistent class static method, the __callstatic method of the class is invoked Object::staticmethod ("static", "not Found methods");
When you output an object directly, it calls the object's __tostring method, Echo $obj;
When an object is used as a function, it calls the object's __invoke method, Echo $obj ("test");
When you clone an object, you call the object's __clone method $obj 2 = Clone $obj;
When determining whether an object property exists, the __isset method of the object is invoked Var_dump (Isset ($obj->aaa));
 
 
When the object property is destroyed, the object's __unset method unset ($obj->aaa) is invoked;
 
  Class object{Protected $array = Array (); function __construct () {echo __method__.
  You are instantiating objects <br> ";
  function __set ($name, $value) {$this->array[$name]= $value;
  function __get ($name) {return $this->array[$name];
    function __call ($name, $arguments) {var_dump ($name, $arguments);
  Return "Magic function\n"; The static function __callstatic ($name, $arguments) {Var_dump ($Name, $arguments);
  Return "Magic static function"; function __tostring () {return __class__. '
  <br> ';
    function __invoke ($arguments) {var_dump ($arguments); return __method__. '
  <br> '; The function __clone () {echo __method__.
  You are cloning objects <br> "; The function __isset ($name) {echo __method__. You want to judge if there is any attribute ". $name."
    <br> ";
  return 1; The function __unset ($name) {echo __method__. You want to delete the attribute ". $name."
  <br> "; The function __destruct () {echo __method__.
  You are cancelling object <br> ";

 }
 
 
 
}

The above is about the common magic method in PHP all the content, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.