This article mainly introduced the PHP class related knowledge points, combined with the example form summarizes the PHP class related final class, the final method, the abstract class, the abstract method, the interface, the overload, the Magic method and so on related concepts and the Operation skill, needs the friend to refer to the next
This paper summarizes the relevant knowledge points of PHP class. Share to everyone for your reference, as follows:
Final class and final method
If a method in the parent class is declared final, the child class cannot overwrite the method. If a class is declared final, it cannot be inherited.
Final class A{}class a{ final public Function A () {}}
Abstract classes and abstract methods
Abstract Class A {public abstract function func ();} Class A extends a{public function func () {}}
① A class can be declared as an abstract class using abstract, which is a class that cannot be instantiated and is used only as a parent class for other classes.
Abstract class name{}
② a method can be declared as an abstract method using abstract, and an abstract method only needs to declare the method header, without the method body of the curly brace part.
Abstract public Function name ($val);
③ There is an abstract method in a class, the class must be declared as an abstract class.
④ subclasses inherit from an abstract class, the subclass does not implement all the abstract methods in the parent class, unless the subclass also continues as an abstract class.
When the ⑤ subclass implements a method that abstracts the parent class, the access control range cannot be reduced, and the parameters of the method must also be consistent (overridden).
Interface
Interfaces can be thought of as higher-level abstractions of abstract classes, which can be described as exceptions to abstract classes
Interface a{ function func ();} Class A implements a { function func () {}}
Classes: Properties, Class constants, methods
Abstract classes: Properties, class constants, abstract methods
Interfaces: Interface constants, abstract methods
① interface enables ' multiple inheritance ' compared to abstract classes
Class A implements a,b,c{}
② an interface can inherit only one interface
Interface a extends b{}
③ methods are abstract methods that do not have to be abstracted, that is, the subordinate class either implements the interface (implements), or it is also declared as an abstract method
Heavy-duty Technology
1. Property overloading: If a non-existent property is used, a predefined method in the class is invoked to process the data;
For scenarios where the attribute is not used, do the following
①: Get () in the auto-call class
② Assignment: Set () in the auto-call class
③ determine if there is a isset () in the auto-call class
④ destroy: Automatically call Unset () in class
The premise is to define these methods in advance, otherwise you will get an error.
<?phpclass bee{public $ol = Array (); Public function Get ($key) { if (isset ($this->ol[$key]) { return $this->ol[$key]; } return $key. ' Not exists '; } Public function set ($key, $val) { $this->ol[$key] = $val; } Public function Isset ($key) { if (isset ($this->ol[$key])) { return true; } return false; } Public function unset ($key) { unset ($this->ol[$key]);} } $obj = new Bee (); Echo $obj->one;echo ' <br> '; $obj->one = 1;echo $obj->one;echo ' <br> '; Var_dump ( Isset ($obj->one));
The results of the operation are as follows:
One not Exists1boolean true
2. Method overloading: If a non-existent method is used, a pre-defined method in the class is invoked to handle the behavior;
For application scenarios where the method is not used, do the following
① calling an inaccessible method in an object: Call ()
② non-accessible method called in static mode: Callstatic ()
<?phpclass bee{public function call ($name, $arr) { echo ' <br> '; echo ' function '. $name. ' not exist '; } static function Callstatic ($name, $arr) { echo ' <br> '; echo ' function '. $name. ' not exist '; } /*public function Call ($name, $arr) { $num = count ($arr); if ($num <1) { $name (); } else{ $name = $name. ' _one '; $name ($arr [0]); } */} $obj->func (); Bee::stafunc ();/* $obj->func (1); function func () { echo ' <br> '; Echo ' no.0 ';} function Func_one ($val) { echo ' <br> '; Echo ' No. ' $val;} */
The results of the operation are as follows:
function func not existfunction stafunc not exist
Note: The above methods are magic methods, all with parameters