A simple article on PHP object-oriented concepts and examples can be used as a reference for anyone who needs it.
Keywords and special Variables
New, class, extends. Everyone knows about these three items.
:, The range resolution operator (also known as Paamayim Nekudotayim) or, more simply, a colon, can be used to access static members, methods, and constants. It can also be used to override the members and methods in the class.
Parent and self. Parent refers to the name of the base class that the derived class refers to in the extends declaration. This avoids the use of the base class name in multiple places.
$ This pseudo variable. $ This points to the current instance. $ This is not necessarily the object to which the method belongs. Sometimes the code in Class A calls A static method in Class B. Example: php.net/manual/zh/language.oop5.basic.php "> http://www.php.net/manual/zh/language.oop5.basic.php
Static keyword. If the declared class member or method is static, you can directly access it without instantiating the class. However, apart from static methods, you cannot access static members through an object. $ This is not used in static methods. Use self ::.
Final keyword. It can act on classes and functions, so that classes cannot be inherited and methods cannot be overwritten.
Attribute
It can be initialized, but the initialization value must be a constant. The const keyword is used before a constant. The constant value must be a fixed value. It cannot be the result of a variable, class attribute, or other operations (such as function call.
Constructor and destructor
Neither of these functions secretly calls the response functions of the base class, which is different from the java constructor mechanism. To achieve this effect, the execution must be displayed. Exceptions cannot be thrown in destructor.
Abstract class: an abstract class method cannot contain specific implementations, and an abstract class cannot be instantiated. The Child class must be inherited first and then instantiated. In addition, the sub-class access control should be the same as the abstract class, or be more relaxed. An abstract class contains at least one abstract method.
Interface
Using interfaces, you can specify the methods that a class must implement, but you do not need to define the specific content of these methods.
All methods defined must be public and the method is empty.
A constant can be defined, but no attribute exists.
The implementation of the interface (implements) must implement all methods and multiple interfaces can be implemented (note that methods cannot be renamed ).
Interfaces can be inherited by other interfaces (extends)
The Code is as follows: |
Copy code |
<? /* * Defines the User interface. * And subclass NormalUser, VipUser, InnerUser */ // The User interface defines three abstract methods. Interface User { Public function getName (); Public function setName ($ _ name ); Public function getDiscount (); } Abstract class AbstractUser implements User { Private $ name = ""; // name Protected $ discount = 0; // discount Protected $ grade = ""; // level Public function _ construct ($ _ name ){ $ This-> setName ($ _ name ); } Public function getName (){ Return $ this-> name; } Public function setName ($ _ name ){ $ This-> name = $ _ name; } Public function getDiscount (){ Return $ this-> discount; } Public function getGrade (){ Return $ this-> grade; } } Class NormalUser extends actuser { Protected $ discount = 1.0; Protected $ grade = "NormalUser "; } Class VipUser extends actuser { Protected $ discount = 0.8; Protected $ grade = "VipUser "; } Class InnerUser extends actuser { Protected $ discount = 0.7; Protected $ grade = "InnerUser "; } ?> |
Product. php
The Code is as follows: |
Copy code |
<? Include_once ("User. php "); Include_once ("Product. php "); // How much does it cost to buy a product? Class ProductSettle { Public static function finalPrice (User $ _ user, Product $ _ product, $ number = 1 ){ $ Price = $ _ user-> getDiscount () * $ _ product-> getProductPrice () * $ number; Return $ price; } } ?> |
The example below is implementation. You can analyze it by yourself.
The Code is as follows: |
Copy code |
<? Include_once ("./class/User. php "); Include_once ("./class/Product. php "); Include_once ("./class/ProductSettle. php "); $ Number = 10; $ Book = new BookOnline ("design mode "); $ User = new NormalUser ("Tom "); $ Price = ProductSettle: finalPrice ($ user, $ book, $ number ); $ Str = "Hello, dear user". $ user-> getName (). "<br> "; $ Str. = "your level is". $ user-> getGrade (). ", <br> "; $ Str. = "your discount is". $ user-> getDiscount (). "<br> "; $ Str. = "buy $ number book". $ book-> getProductName (); $ Str. = "" price is $ price <br> "; Echo $ str;
$ User = new vipUser ("Tom "); $ Price = ProductSettle: finalPrice ($ user, $ book, $ number ); $ Str = "Hello, dear user". $ user-> getName (). "<br> "; $ Str. = "your level is". $ user-> getGrade (). ", <br> "; $ Str. = "your discount is". $ user-> getDiscount (). "<br> "; $ Str. = "buy $ number book". $ book-> getProductName (); $ Str. = "" price is $ price <br> "; Echo $ str;
$ User = new InnerUser ("Tom "); $ Price = ProductSettle: finalPrice ($ user, $ book, $ number ); $ Str = "Hello, dear user". $ user-> getName (). "<br> "; $ Str. = "your level is". $ user-> getGrade (). ", <br> "; $ Str. = "your discount is". $ user-> getDiscount (). "<br> "; $ Str. = "buy $ number book". $ book-> getProductName (); $ Str. = "" price is $ price <br> "; Echo $ str; ?> |