The object-oriented features of php are the same as those of java, except that one is a weak language and the other is a strong language. there are still many differences in the implementation of the two, and the functions can only be reached, since the two are often mixed up, I wrote this article to summarize some of php's object-oriented
The object-oriented features of php are the same as those of java, except that one is a weak language and the other is a strong language. there are still many differences in the implementation of the two, and the functions can only be reached, since the two are often mixed up, I wrote this article to summarize some of php's object-oriented knowledge points for future queries. class Person {var $ name; private $ age; function Person () {// The old constructor method. the method name and class name are the same. // The constructor automatically executes the function when the object is generated. it is generally used to initialize the variable} function _ construct () {// New PHP 5 constructor} function _ destruct () {// destructor // The function automatically executed after the object is referenced (the reference is placed in the stack, object in heap)} private function study () {// The keyword private is used to declare private attributes or methods. private attributes or methods cannot be accessed externally, // only accessible in the class, and private attributes and methods cannot be inherited} protected function study () {// The keyword protected is used to declare protected attributes or methods, protected attributes or methods cannot be accessed externally, // they can only be accessed in the class, but protected attributes or methods can be inherited} public function study () {// public declares a total of methods or attributes. unspecified functions or methods are default public} common system methods _ set (), _ get (), _ isset (), _ unset () ;__ toString (), _ clone (), _ call (), _ autoload (); function _ get ($ name) {// This function is automatically called when an external user attempts to access the private attributes of an object, and the accepted parameter is the attribute name} function _ set ($ name, $ value) for external access. {// this parameter is automatically called when an external user tries to set the private attribute of an object, and the accepted parameter is the property name and value} function _ isset ($ name) {// when the external use of isset () function to test whether a private property of an object is executed when it exists. The property name} function _ unset ($ name) is automatically tested {// When unset () is used externally () this function is automatically called when you try to delete a private property of an object. it automatically accepts the property name} function _ toString () {// If you directly output an object reference (pointer) will automatically call this function return str; // A string must be returned} function _ clone () {// if the clone keyword is used externally to clone an object ($ a1 = clone $ a2), this function is automatically called. // This function can be used for initialization, in this case, $ this indicates the newly cloned object $ a1, indicating that the cloned object $ that // $ a1 = clone $ a2 is different from $ a1 = $ a2, the former creates a new object in the heap, and the latter only points to the same object with a pointer.} function _ call ($ funName, $ argus) {// This function is automatically called when a method does not exist in an object is called. The $ funName parameter (the function name does not exist) is automatically accepted. // The parameter $ argus is an array }__ autoload () function: This function is not written in the object. it is written in the top of a php file to automatically load the class function _ autoload ($ className) {include $ className. "php" // assembled into a complete path} inherited: class Boy extends Person {var $ hoby; function playgame () {}} php does not support function overloading, due to the weak type, the parameter type is uncertain. because php supports function dynamic parameters, the number of parameters is uncertain. Therefore, php can only overwrite function (method override) method rewriting: add the paret: method name (); in the subclass method, add the code in the method of the parent class to the common keyword in the new method of php object-oriented: final static constfinal class Person {// the class with the keyword final cannot be inherited from final function study () {// The function with the final keyword cannot be overloaded (overwritten )}} static modifier methods or attributes can directly use the class name: Method () or class name: property static method without instantiating objects. The non-static attribute const cannot be used to modify constants: const NUM = 100; constant names are used to uppercase keywords $ this and self. The former represents the current object, and the latter represents the serialization and deserialization of the class object: serialize (value) the parameter is the object name, and the return value of the function is a string Deserialization: the unserialize (str) parameter is the string, and the return value of the function is when the object is used: 1. during network transmission, 2. partially serialized when writing an object into a file or database: Add this method to the object: function _ sleep () {$ arr = array ("name ", "age"); // The attribute to be serialized is passed in the form of an array return $ arr;} this method is automatically called when the deserialization is performed (if the object is not written, it is not called): function _ wakeup () {// Similar To _ clone (), mainly for Initialization} abstract methods and abstract classes: abstract methods: abstract function abd (); only the method name is available. classes that contain abstract methods are called abstract classes. you also need to use abstract keywords to modify abstract classes. abstract classes that are added to a common class cannot be instantiated, classes that inherit abstract classes only as one canonicalized must implement the abstract methods in them. Otherwise, they are also abstract classes and cannot instantiate object interfaces: interface declaration interface demo {} all member attributes must be constants all methods are abstract all members must be public one class can only inherit one parent class, but multiple interfaces can be implemented. The polymorphism (different from java) in the class one implements two {} php is usually implemented in php as follows: Normally, the parent class is an abstract class or the interface is implemented by a subclass, the subclass contains the same method name, but the implementation of each subclass is different.