Object-oriented in my opinion is a variety of PHP classes, methods, functions, the following I would like to introduce you to the PHP object-oriented tutorial, you are interested in reference.
The first case: When a subclass does not have a constructor defined, it inherits by default.
The second case: the subclass defines the constructor and is not inherited.
For 4.x, if the parent class exactly defines a function with the same name as the subclass, it is treated as a constructor for the subclass:
The code is as follows |
Copy Code |
Class A { function A () { echo "I am the constructor of A. n "; } function B () { echo "I am a regular function named B in Class A. n "; echo "I am not a constructor in a. n "; } } Class B extends A { function C () { echo "I am a regular function. n "; } } PHP4 would call B () $b = new B; |
The above code calls a in PHP5 and does not call B ()
PHP Object-oriented: interface instance
Design an online sales system, the user part is designed as follows:
Divides the user into, Normaluser, Vipuser, inneruser three kinds.
Requires the user to calculate the price of the product according to different discounts.
and requires space for future expansion and maintenance.
The user section first declares an interface user, which is implemented by users.
user.php
The code is as follows |
Copy Code |
/* * Defines the user interface. * and sub-class Normaluser,vipuser,inneruser */ User interface, which 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 Abstractuser { protected $discount = 1.0; protected $grade = "Normaluser"; } Class Vipuser extends Abstractuser { protected $discount = 0.8; protected $grade = "Vipuser"; } Class Inneruser extends Abstractuser { protected $discount = 0.7; protected $grade = "Inneruser"; } ?> |
As for the product, we have designed the following.
Declares an interface product, and then inherits the next book interface from product.
The books that are sold online are finally the Bookonline class that implements the book interface.
product.php
The code is as follows |
Copy Code |
< p>!--? /* product-related class put. */ Interface product{//Define Product Interface Public function getproductname (); Public function GetProductPrice (); } Interface book extends product {//book is a category of products Public function getauthor (); } Class Bookonline implements book{//define book class. private $productName;//Product name private $productPrice;//Product price P Rivate $author; Author Public Function __construct ($_bookname) { $this->productname = $_bookname; //Place the associated initialization code here. The code associated with the database. } Public function Getproductname () { return $this->productname; } Public Function GetProductPrice () { //The price is read from the database here. //Suppose the price is 100 yuan. $this->productprice = +; Re Turn $this->productprice; } Public function Getauthor () { //values from the database. return $this->author; } } ? |
For settlement, we use a separate settlement class, which is calculated using static methods. Product settlement. Note the parameter type.
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 following example is an implementation. We can analyze it by ourselves.
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 (). " "; $str. = "Your level is". $user->getgrade (). ", "; $str. = "Your discount is". $user->getdiscount (). " "; $str. = "Purchase $number this". $book->getproductname (); The price of $str. = "is $price
"; Echo $str;
$user = new Vipuser ("Tom"); $price = Productsettle::finalprice ($user, $book, $number); $str = "Hello, dear User". $user->getname (). " "; $str. = "Your level is". $user->getgrade (). ", "; $str. = "Your discount is". $user->getdiscount (). " "; $str. = "Purchase $number this". $book->getproductname (); The price of $str. = "is $price
"; Echo $str;
$user = new Inneruser ("Tom"); $price = Productsettle::finalprice ($user, $book, $number); $str = "Hello, dear User". $user->getname (). " "; $str. = "Your level is". $user->getgrade (). ", "; $str. = "Your discount is". $user->getdiscount (). " "; $str. = "Purchase $number this". $book->getproductname (); The price of $str. = "is $price
"; Echo $str; ?> |
http://www.bkjia.com/PHPjc/628660.html www.bkjia.com true http://www.bkjia.com/PHPjc/628660.html techarticle object-oriented in my opinion is a variety of PHP classes, methods, functions, the following I would like to introduce you to the PHP object-oriented tutorial, you are interested in reference. The first case ...