First, the constructor function, the destructor
(1) constructor: __construct ()
Description: Called when an object is instantiated, with parameters
Cases:
$obj = new A ($a, $b);
(2) destructor: _destruct ()
Description: The page execution ends the call. Used to clean Up program resources
Ii. references to Objects
(1) Different references, the same object:
$a = new A; $b = $c = $a;//Like A, B, C (object alias) share a House (object) unset ($a);//Object not destroyed unset ($a, $b, $c);//Object destruction
(2) The same reference, sharing a variable, except that the name is different:
$a = new A; $d = & $a; unset ($a);//Object Destruction unset ($b);//Object destruction
(3) Copying objects, different objects:
$a = new A; $b = clone $a;
Iii. Inheritance: Extends
Description
(1) Single inheritance, extends can only be followed by a parent class
(2) The overridden method must be exactly the same as the original method (parameter, type)
Pros: Subclass members do not have to be defined repeatedly
Cases:
Class Zi extends fu{ protected function Cry () { parent::cry ();} }
Four, access control, three kinds of access rights:
(1) Public: Publicly available, accessible anywhere
(2) Protected: Protected, can be accessed by itself and sub-class
(3) Private: Privately, can only be accessed by itself
V. Static members: Statics
(1) Static properties are used to hold common data for a class
(2) and classes are stored together, not with objects, so you can call directly without instantiating it.
(3) Static methods can only access static properties, cannot use this.
Self:: can also access its non-static members
Cases:
Class A {public static $a =1; Const STR = ' str ';//constants, not allowed to modify}
Called in a class: Self:: $a (itself, parent), or static:: $a (self)
Calling outside the class: A:: $a
$AC = new A; $BC = new A; $AC:: $a =2;echo $BC:: $a;
Vi. Final Keywords
(1) A method in a subclass that exactly matches the parent class method name can complete the override of the parent class method (with no parameters).
(2) The parent class method is added to the final keyword before it can be overridden in a subclass.
(3) For parent classes that do not want to be inherited by any class, you can add the final keyword before the parent classes class.
(4) Add the Final keyword class/method, but cannot be overridden/inherited, but can call
Cases:
Final Class A { final public function A () {}}
Seven, Interface: interface
(1) Public only
(2) Only constants can be declared
(3) All abstract methods
(4) class must implement all methods of interface
(5) interface can extends (inherit) multi-interface
(6) class can be implements (implemented) multi-interface
(7) can use $ A instanceof A to determine the object when the interface is implemented
(8) The overridden method must be exactly the same as the original method (parameter, type)
Advantages: Free combination of functions. For example, the user: login, send messages, upload pictures, etc., put in a class too large and inflexible, can be made into an interface, use the time to adjust; registration: Verify mailbox, encrypt.
Cases:
Interface a {public function Saya ();} Interface B {public function Sayb ();} Interface C {public function sayc ();} Interface D extends A, b {public function Sayd ();} Class Fu implements d,c{public function Saya () {} public function Sayb () {} public function Sayc () {} Public Function Sayd () {}}
VIII. Abstract Classes: Abstracts
(1) The abstract keyword is used to define abstraction classes
(2) Add the abstract keyword before the abstraction method, no specific implementation is required
(3) Abstract class can contain ordinary methods, the specific implementation of the method
(4) Subclasses of inherited abstract classes need to implement abstract methods defined in abstract classes
Pros: Define class specifications. For example, define SMS specification to connect different service providers, define database specification to connect different databases
Cases:
Abstract Class A { abstract public Function A (); Public B () {}}
Nine, Magic method
(1) __tostring (): The method is called automatically when the object is used as a string
Cases:
echo $obj;p ublic function __tostring () { return ' __tostring ';}
(2) __invoke (): The method automatically calls when the object is called as a method
Cases:
$obj (4);
Overloads of the method:
(3) __call ($a, $arr): Object calls a method that does not exist automatically calls
(4) __callstatic ($a, $arr): An object calls an automatic static method that does not exist, and the method must add the keyword static
Example: function static __callstatic () {}
Overloading of properties
(5) __set (): __set () is called when assigning a value to an inaccessible property: function __set ($name, $value)
(6) __get (): When reading the value of an inaccessible property, __get () is called as an example: function __get ($name)
(7) __isset (): __isset () is called when isset () or empty () is called on a non-accessible property
(8) __unset (): __unset () is called when unset () is called on an inaccessible property
The so-called inaccessible attribute, in effect, is that when a property is called, the attribute is not defined, and different actions trigger different magic methods.
(9) __clone (): Automatically called when object is clone
Pros: Customize which properties are copied so that some properties of the copied object are not copied past.
Cases:
Obj1 = Clone $obj; Implementing object replication to two objects of the same value
PHP Object-oriented notes