A concise summary of the Magic method in the PHP class, Magicmethod
1. __construct () and __destruct ()
It is called when the instance is created/destroyed, and can pass 0 or more parameters.
Class A { function a () { echo "build a"; } function __destruct () { echo "destroy a"; }} $obj = new A ();//unset ($obj);
Note:the destructor method would be called as soon as there is no other references to a particular object, or in any order During the shutdown sequence.
With regard to constructors, PHP5.3.3, a method defined in a class named in a particular namespace is no longer considered a constructor. The constructor is still the same as the original in the namespace-free class. Such as:
Namespace Foo;class Bar {public function bar () { //treated as constructor in PHP 5.3.0-5.3.2 //treated as Regular method as of PHP 5.3.3 }}
If there is no namespace Foo; Bar () will also be used as a constructor. In addition, if the following conditions exist:
function __construct () { echo "construct A"; } function A () { echo "build A"; } function __destruct () { echo "destroy A"; }}
That is, contains both __construct () and a function with the same name as the class name, then only __construct () is called.
2. __call () and __callstatic ()
This method is called when an attempt is to invoke a nonexistent method. Two parameters, one is the method name, and the other is the parameter array of the called method.
Class methodtest{public function __call ($name, $arguments) { //Note:value of $name are case sensitive.
echo "Calling object method ' $name '" . Implode (", $arguments). "
"; } public static function __callstatic ($name, $arguments) { //Note:value of $name are case sensitive. echo "Calling static method ' $name '" . Implode (", $arguments). "
"; }} $obj = new Methodtest; $obj->runtest (' in ', ' object ', ' context '); Methodtest::runtest (' In ', ' static ', ' context ');
Where $arguments is passed in as an array. Operation Result:
Calling object method ' Runtest ' in object context
Calling static method ' Runtest ' in static context
Also note the scope of the function protected and private:
Class Testmagiccallmethod {public function foo () { echo __method__. Php_eol. "
"; } Public Function __call ($method, $args) { echo __method__. Php_eol. "
"; if (Method_exists ($this, $method)) { $this-$method (); } } protected function Bar () { echo __method__. Php_eol. "
"; } Private function Baz () { echo __method__. Php_eol. "
"; }} $test = new Testmagiccallmethod (); $test->foo ();/** * Outputs: * Testmagiccallmethod::foo */$test Bar ();/** * Outputs: * Testmagiccallmethod::__call * testmagiccallmethod::bar * * $test->baz ();/** * Outputs: * Testmagi Ccallmethod::__call * Testmagiccallmethod::baz * *
3.__get () and __set ()
Called when attempting to read a property that does not exist for an object.
Note: We can use this function to implement various actions like reflection in Java.
Class test{public function __get ($key) { echo $key. "Not exists"; } Public Function __set ($key, $value) { echo $key. "=". $value; }} $t = new Test (); Echo $t->name. "
"; $t->name =" abc ";
Output:
Name NOT EXISTS
name = ABC
4. __tostring ()
This method is similar to the ToString () method of Java, and when we print the object directly, the callback uses this function, and the function must return a string.
Class test{ Private $name = "abc"; Private $age = n; Public Function __tostring () { return ' name: $this->name, Age: $this->age "; }} $t = new Test (); Echo $t;
Output:
NAME:ABC, Age:12
Explain in detail the Magic method in PHP5
The kernel of the PHP Processing Object section has been completely re-developed, providing more functionality while also improving performance. In previous versions of PHP, objects were handled the same way as basic types (numbers, strings). The flaw in this approach is that when an object is assigned to a variable, or when an object is passed through a parameter, the object is completely copied. In the new version, the above operation will pass the reference (which can be interpreted as an identifier of the object), not a value.
Many PHP programmers may not even be aware of the way old objects are handled. In fact, most PHP applications can run well. Or just need a little change.
Private and protected members
PHP5 introduces the concept of private and protected member variables. We can use it to define the visibility of class members.
Example
Protected members can be accessed by subclasses, while private members are accessible only to the class itself.
Code:--------------------------------------------------------------------------------
Class MyClass {
Private $Hello = "Hello, world!\n";
protected $Bar = "Hello, foo!\n";
protected $Foo = "Hello, bar!\n";
function Printhello () {
Print "MyClass::p Rinthello ()". $this->hello;
Print "MyClass::p Rinthello ()". $this->bar;
Print "MyClass::p Rinthello ()". $this->foo;
}
}
Class MyClass2 extends MyClass {
protected $Foo;
function Printhello () {
MyClass::p Rinthello (); /* Should print */
Print "MyClass2::p Rinthello ()". $this->hello; /* shouldn ' t print out anything */
Print "MyClass2::p Rinthello ()". $this->bar; /* shouldn ' t print (not declared) */
Print "MyClass2::p Rinthello ()". $this->foo; /* Should print */
}
}
$obj = new MyClass ();
Print $obj->hello; /* shouldn ' t print out anything */
Print $obj->bar; /* shouldn ' t print out anything */
Print $obj->foo; /* Should ... Remaining full text >>
PHP Magic Method
This is for security, that is, the concept of object-oriented encapsulation, if you set public directly then no object can freely set the value of this property, and is not determined by the task logic, so detours can increase security
http://www.bkjia.com/PHPjc/840759.html www.bkjia.com true http://www.bkjia.com/PHPjc/840759.html techarticle A concise summary of the Magic method in the PHP class, Magicmethod 1. __construct () and __destruct () are called when the instance is created/destroyed, and can pass 0 or more parameters ...