PHP Common Magic Method Summary, PHP Magic Summary _php Tutorial

Source: Internet
Author: User

PHP Common Magic Method Summary, PHP Magic Summary


This article in detail in PHP commonly used magic method to summarize, share to everyone for your reference, the specific content as follows

1, PHP to All "__" method as a magic method, so any custom method can not be "__" start
The overloads provided by PHP refer to the dynamic creation of properties or methods. Overloading is accomplished by means of magic. The parameters of these magic methods cannot be consumed by passing, __get (), __set (), __isset (), __unset (), implementing Class property overloading.
2. The __get () method is called when accessing a property that is not accessible in a class
3. When accessing an inaccessible property in a class, the __get (), __set (), __isset (), __unset () are called depending on the situation, and isset () is invoked when the property is accessed using the function __isset () or empty () The __unset () method is called when the method uses the unset () function.
4, these four methods cannot use the static keyword definition, after php5.3.0, a warning level warning will occur when using the static method to define the 4 methods.
5. What is an inaccessible attribute: (1) A property that is not in the class, and (2) a private variable is accessed outside the class. Method overloads, implemented by __call () and __callstatic (), are the differences between whether a context is a static method, which is called when an inaccessible static method is called. __callstatic () is only added in php5.3.0.
6, __sleep () and __wakeup ()
When Serialize () is serialized, it first checks for the presence of the __sleep () function, and if so, calls __sleep (), returns an array containing all the variable names, and if the method does not return anything, NULL is serialized and produces a notice-level error.
The __wakeup () method, in contrast to __sleep (), is called when Unserialize () is called
7, __tostring (), let a class determine how it is converted to a string, before php5.2.0, this method can only be combined with the Echo or Print method, php5.2.0, can be combined with the printf () decoration, but the decoration of the time may not use a similar% Non-character modifiers such as D.
8, __invoke (), when attempting to invoke the function in the way the object is called, will start this function, only after the php5.3 to take effect.
9, __set_state () static method, when called Var_export (), will call this static method, php5.1.0 after the effective. The only parameter is to receive an array.
10, __construct () and __destruct (), constructors and destructors, constructors are primarily used for initialization of classes, and destructors run when all references to an object are deleted or destroyed when the object is displayed. An exception is not allowed in the destructor, otherwise a fatal error is set, and the destructor is invoked at the end of the script, when all HTTP headers have been sent.
11, __clone (), object replication, this method cannot be called directly, must be displayed through the Clone keyword to call

Code:

When an object is instantiated, the object's __construct method is called $obj = new Object (),//The __set method of the object is called when a value is assigned to an object that does not exist $obj->title = "Hello";//When using a property that does not exist for the object, The object's __get method is called when the Echo $obj->title;//method that calls the object does not exist, the __call method of the object is called $obj->test ("Hello", 123,567);//When a class static method that does not exist is called The __callstatic method of the class is called Object::staticmethod ("static", "not Found Method");//The __tostring method of the object is called when the object is directly output echo $obj; When an object is used as a function, the object's __invoke method is called the Echo $obj ("test"), or the __clone method of the object is called when the object is cloned $obj2 = clone $obj;//The object's __ is called when the object property is determined to exist.  The Isset Method Var_dump (Isset ($obj->aaa)),//Destroys the object's properties, invokes the __unset method of the object unset ($obj->aaa);   Class object{Protected $array = Array (); function __construct () {echo __method__. You are instantiating an object
"; } 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"; } static function __callstatic ($name, $arguments) {var_dump ($name, $arguments); Return "Magic static function"; } function __tostring () {return __class__. '
'; } function __invoke ($arguments) {var_dump ($arguments); return __method__. '
'; } function __clone () {echo __method__. You're cloning an object.
"; } function __isset ($name) {echo __method__. You want to determine if there is a property ". $name."
"; return 1; } function __unset ($name) {echo __method__. You want to delete the attribute ". $name."
"; } function __destruct () {echo __method__. You are Unregistering objects
"; } }

The above is about the common magic method in PHP all the content, I hope that everyone's learning has helped.

Articles you may be interested in:

    • Instructions for using the PHP magic method
    • Explanation of PHP's Magic Methods __get () and __set () usage Introduction
    • Based on the PHP5 Magic constants and the magic method of the explanation
    • A detailed explanation of the magic method in PHP5
    • Summary of common magic methods in PHP
    • A concise summary of the Magic method in the PHP class
    • Php Magic Method __get, __set use instance
    • Php Magic Method __isset, __unset use instance
    • PHP Common Magic Method of explanation

http://www.bkjia.com/PHPjc/1099054.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099054.html techarticle PHP Common Magic Method Summary, PHP Magic Summary of this article in detail in PHP commonly used magic method to summarize, share to everyone for your reference, the specific content is as follows 1 、...

  • 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.