: This article mainly introduces (2) PHP object-oriented Theory 2. For more information about PHP tutorials, see. I. magic methods:
1. the magic method starts with "_" and is the syntactic sugar of PHP. Syntactic sugar is a more practical encoding method or technique that makes the code easier to read.
2. _ set and _ get
$name = $value; } public function __get($name){ if (!isset($this->$name)){ echo 'no set '; $this->$name = 'set default value:'; } return $this->$name; }}$a = new Account();echo $a -> user;echo "
";$a->user = 5;echo $a->$name;echo "
";echo $a->big;If the _ set and _ get magic methods are defined in the class, when an object is assigned a value or a value, no error is reported even if the attribute does not exist. Enhance program robustness.
2. _ call and _ callStatic:
(The following code failed to be executed)
Fieldvalues [$ fieldname];} static function _ callStatic ($ method, $ args) {$ field = preg_replace ('/^ findBy (\ w *) $ ', '$ {1}', $ method); $ query = "select * from ". static: $ table. "where $ field = '$ args [0]"; return self: createDomain ($ query);} private static function createDomain ($ query) {echo "test "; $ klass = get_called_class (); $ domain = new $ klass (); $ domain-> fieldvalues = array (); $ domain-> select = $ query; foreach ($ klass:: $ fields as $ field =>$ type) {$ domain-> fieldvalues [$ field] = 'Todo: set from SQL result';} return $ domain ;}} class Customer extends ActiveRecord {protected static $ table = 'custdb'; protected static $ fields = array ('id' => 'int', 'Email '=> 'varchar ', 'lastname' => 'varchar '); public function _ construct () {echo "***";}} class Sales extends ActiveRecord {protected static $ table = 'salesdb'; protected static $ fields = array ('id' => 'int', 'item' => 'varchar ', 'qty '=> 'int');} assert ("select * from custdb where id = 123" = Customer: findById (123)-> select ); assert ("TODO: set from SQL result" = Customer: findById (123)-> email); assert ("select * from salesdb where id = 321" = Sales:: findById (321)-> select); assert ("select * from custdb where lastname = 'denoncourt'" = Customer: findByLastname ('denoncourt')-> select );You can use _ call and _ callStatic to 'prevent an error from calling a non-existent method '. It can also make the dynamic creation of methods possible.
3. _ toString
User}, the password is {$ this-> pwd} ";}}$ a = new Account (); echo $ a; echo"
"; Echo PHP_EOL." = "; echo"
"; Print_r ($ );In fact, the toString method is also a serialization method.
II. Inheritance and polymorphism:
1. Inheritance: there is a parent-child relationship between the class and the class. the subclass inherits the attributes and methods of the parent class, which is called inheritance.
In inheritance, child classes have methods and attributes of the parent class, and child classes can also have their own methods and attributes.
Name, "\ tis", $ this-> gender, "\ r \ n" ;}} class family extends person {public $ name; public $ gender; public $ age; static $ money = 100000; public function _ construct () {parent ::__ construct (); echo "here is a subclass", PHP_EOL;} public function say () {echo"
I said ". $ this-> name," \ tis \ t ", $ this-> gender,", and is \ t ", $ this-> age, PHP_EOL ."
";}Public function cry () {echo parent: $ money, PHP_EOL; echo" %>-<% ", PHP_EOL; echo self: $ money, PHP_EOL; echo "(* ^_^ *)";} public function read () {echo"
Read again ". parent: say ()."
";}}$ Poor = new family (); $ poor-> name = 'Lil'; $ poor-> gender = 'female '; $ poor-> age = 25; $ poor-> say (); $ poor-> cry (); $ poor-> read ();/** question: * I added a method named read to the subclass. * The result is: * Here is the parent class. here is the Subclass. I said Lee is female, and is 2510000%>-<% 100000 (* ^_^ *) why is Lee isfemaleread again behind lee is female ...... (I think read again should be executed first )*/
In inheritance, parent refers to the parent class and self refers to itself. Use ":" (range resolution operator) to call the method of the parent class ." : "Operator is also used to call class constants and static methods.
If the declared class member or method is static, you can directly access it without instantiating the class.
You cannot access a static member of an object (except for static methods) or use ":" to access a non-static method.
Inheritance is a kind of "Yes, like" relationship, while combining a kind of "need" relationship.
From the perspective of method reuse, if two classes have many identical code and methods, you can abstract a parent class from these two classes, provide a public method, and then two classes as subclasses. Provides personalized methods.
Inheritance issues:
A. inheritance destroys encapsulation.
B. Inheritance is tightly coupled.
C. complex inheritance extension.
D. improper use of inheritance may violate the logic in the real world.
car = new car; } public function addoil(){ $this->car->addoil(); }}$bmw = new bmw();$bmw ->addoil();$benz = new benz();$benz->addoil();Use cases of inheritance:
A. The abstract layers of the inheritance tree are generally no more than three layers.
B. The use of final modifiers for classes not specifically used for inheritance can prevent important methods from being overwritten.
C. The combination relationship can improve code reusability.
D. subclass is a special type, not just a role of the parent class.
E. use combinations of underlying code to improve efficiency. use inheritance of top-layer (business layer) code to improve flexibility.
2. polymorphism:
In actual development, as long as you care about the programming of an interface or base class, you do not have to care about the specific class of an object.
Working () ;}} doprint (new teacher (); doprint (new coder (); doprint (new employee (); doprint (new readBooks ());
Polymorphism can be achieved through the interface.
Summary:
A. polymorphism refers to the embodiment of the same class object at runtime.
B. the PHP language is weak and its implementation of polymorphism is simpler and more flexible.
C. type conversion is not a polymorphism
D. in PHP, the parent class and child class are regarded as the relationship between 'stepped' and 'stepchild ', and there is an inheritance relationship. Sub-classes cannot be transformed to parent classes.
E. The essence of polymorphism is if... else, but the implementation level is different.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces (2) PHP object-oriented Theory 2, including some content, and hopes to help those who are interested in PHP tutorials.