- oriented
ObjectSpecial Practice
(Only in PHP, other languages for
ObjectNo
- oriented
Object--Magic Method
__construct (), __destruct () constructors and destructors
__tostring ()
__invoke ()
__call (), __callstatic ()
__get (), __set (), __isset (), __unset ()
__clone ()
__tostring ()
When
ObjectThis method is called automatically when used as a string.
Echo $obj;
__invoke ()
When
ObjectThis method is automatically called when the method is called.
$obj (4);
When an object is converted to a string, the public function __tostring () {return ' This is the Class magictest ' is called automatically.} __invoke automatically calls the public function __invoke ($x) {echo "__invoke called with parameter" when the object is treated as a method call. $x. " \ n ";}} $obj = new Magictest (); Echo $obj. " \ n "; $obj (5);? >
Output
This is the Class magictest.
__invoke called with parameter 5
__call ()
When an object accesses a method name that does not exist, the __call () method is automatically called
__callstatic ()
The __callstatic () method is automatically called when an object accesses a static method name that does not exist
These two methods are also used as overloads of the method in PHP (overloading)
Note distinguish overrides (overwrite)
With these two methods, the invocation of the name of the same method can be implemented in a different way
For static method overloading, note that this method needs to be set to staticpublic static function __callstatic ($name, $arguments) {echo "Static calling". $name. "With parameters:". Implode (",", $arguments). " \ n ";}} $obj = new Magictest ();//runtest the names of the two methods may not be identical, but through the Magic method, you can call the $obj->runtest ("Para1", "Para2"); There is no declaration of this runtest method, because there is __call this magic method, can also be called magictest::runtest ("Para1", "Para2"); There is no declaration of this runtest method, because there is __callstatic this magic method, can also be called?>
Output:
Calling Runtest with Parameters:para1,para2
Static calling Runtest with Parameters:para1,para2
__get (), __set (), __isset (), __unset ()
__set () is called when a value is assigned to an unreachable property
When reading the value of an inaccessible property, __get () is called
When Isset () or empty () is called on an inaccessible property, __isset () is called
When Unset () is called on a non-accessible property, __unset () is called
The so-called inaccessible attribute is actually called when a property is found to be not defined, when different manipulations trigger different magic methods
These methods are also known as property overloading of the Magic method
Static methodsOverload, note that this method needs to be set to staticpublic static function __callstatic ($name, $arguments) {echo "Static calling". $name. "With parameters:". Implode (",", $arguments). " \ n ";} Property overload Public Function __get ($name) {return "Getting the". $name. " \ n ";} Public Function __set ($name, $value) {echo ' Setting the property '. $name. "To value". $value. " \ n ";} Public Function __isset ($name) {echo "__isset invoked\n"; return true;} Public Function __unset ($name) {echo ' Unsetting property '. $name. " \ n ";}} $obj = new Magictest (); Echo $obj->classname. " \ n "; ClassName is undefined, but through the Magic method __get, this method seems to be defined as $obj->classname= ' MAGICCLASSX '; By magic Method __get The classname name is defined as Magicclassxecho ' $obj->name is set? '. Isset ($obj->classname). " \ n "; Echo ' $obj->name is empty?" Empty ($obj->classname). " \ n "; unset ($obj->classname);? >
Output:
Getting the property ClassName
Setting the ClassName to value MAGICCLASSX
__isset invoked
$obj->name is set?1//If Return is true, the result is displayed as 1, and if return is false, the result is displayed as empty
__isset invoked
$obj->name is empty? If return is true, the result is empty, and if return is false, the result is displayed as 1
Unsetting Property ClassName
Object -oriented--magic method
__clone ()
Object Programming--Special Practice Day 4 ">
Name= ' TBD '; Block you don't want him to copy past data, block out his data, set his initial value}} $james = new Nbaplayer (); $james->name = ' James '; Echo $james->name. " \ n "; $james 2 = Clone $james; echo" before set Up:james2 ' s: ". $james 2->name." \ n "; $james 2->name= ' James2 '; echo" James ' s: ". $james->name;echo" James ': ". $james 2->name;? >
Output
James
Before set Up:james2 ' S:tbd
James ' S:james
James ' S:james2//james2 data does not affect James ' data
-------------------------------------------------------------------------------PHP for
ObjectThe programming note is complete---------------------------------------------------------------------------------------
The above describes the PHP object-oriented programming-special practice Day 4, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.