Tag:php object-oriented oop
A OOP 's Magic Method PHP introduces a __autoload () built-in method to automatically include class files. __autoload () should be written as a method of a single parameter. When the PHP engine encounters an operation that attempts to instantiate an unknown class, it calls the __autoload () method and passes the class name as a string parameter to it. Function __autoload ($_classname) {require $_classname. Class.php ';} $demo = new computer (); PHP uses the __call () built-in method to mask errors that occur when an object calls a method. The __call () method is called automatically when an object calls a method that does not exist. Private function __call ($_methodname, $args) {echo $_methodname. ' method does not exist ';p rint_r ($args);} $computer->go (' I ', 1, ' know '); PHP uses the __tostring () built-in method to print a reference to an object. An object that does not use __tostring () generates an error that automatically calls the __tostring () method when the object is printed. Class computer {private function __tostring () {return ' Print object ';}} Echo new computer (); PHP can define a __clone () built-in method in the class to adjust the cloning behavior of the object. The __clone () method is automatically executed when an object is cloned, and the copied object can be adjusted within its method body. class computer {public $_name ;p ublic function __clone () {$this->_name = ' IBM ';}} $computer 1 = new computer (); $computer 1->_name = ' dell '; $computer 2 = clone $computer1;echo $computer 2->_name; Two. class functions and Object functions PHP provides a series of powerful functions to detect classes and objects. So that in a third-party system, the runtime knows which is being used. The 1.class_exists () function accepts a string representing the class and checks for and returns a Boolean value. Returns TRUE&NBSP if the class exists, otherwise false. Echo class_exists (' computer '); the 2.get_class () function gets the class name of the object and returns False if it is not an object. Echo get_class ($computer); the 3.get_class_methods () function Gets the method (public) in the class and returns it as an array. Print_r (Get_class_methods ($computer)); the 4.get_class_vars () function gets the field (public) in the class and returns it as an array print_r (the Get_class_vars (' Computer ')); the 5.get_parent_class () function gets the parent class of the subclass if it does not return false. Echo get_parent_class (' Notecomputer '); the 6.interface_exists () function determines whether the interface exists and returns False if there is true. Echo interface_exists (' computer '); the 7.is_a () function determines whether the object is a class or is a parent class of this class, returns ture, otherwise returns false. Echo is_a ($computer, ' computer '); the 8.is_subclass_of () function determines whether the object is a subclass of the class, returns ture, or false. Echo is_subclass_of ($notecomputer, ' computer '); the 9.method_exists () function determines whether an object's method exists, returns ture, or returns false. Echo method_exists ($computer, ' _run '); OOP 's reflection  APIPHP5 class and object functions do not tell us everything inside the class, but only their public members. To fully understand a class, you need to know its private members and protected members, as well as the parameters that its methods expect. For this, useReflection API. 1. Obtain the dump information of the Reflection API $rc = new reflectionclass (' computer '); Reflection::export ($RC); 2. Obtain the Information Reflection::export (New reflectionclass (' Reflection ')) of the class library built into PHP, 3. Get an element in the class $ _rc = new reflectionclass (' computer '); Echo $_rc->getfilename ();echo $_rc-> GetName ();
PHP OOP Object-oriented tools