A simple reference to the PHP object-oriented concepts and examples of the article, the need for a friend can simply refer to.
Keywords and special variables
New,class,extends. These three, we all know.
:: The scope resolution operator (also known as Paamayim Nekudotayim) or, more simply, a pair of colons, can be used to access static members, methods, and constants. It can also be used to override members and methods in a class.
Parent and self. The parent refers to the name of the base class that the derived class refers to in the extends declaration. This avoids using the name of the base class in multiple places.
$this pseudo-variables. $this points to the current instance. $this is not necessarily the object that the method belongs to. Sometimes the code in Class A calls a static method of Class B. Reference example: php.net/manual/zh/language.oop5.basic.php ">http://www.php.net/manual/zh/language.oop5.basic.php
The Static keyword. If you declare a class member or method to be static, you can access it directly without instantiating the class. However, in addition to static methods, you cannot access static members through an object. In a static method, $this is not used. Instead, use self::.
Final keyword. Can be used for classes (class) and methods (function), so that classes cannot be inherited and methods cannot be overwritten.
Property
can be initialized, but the initialized value must be a constant. Constants are preceded by a const keyword, and the value of the constant must be a fixed value and cannot be the result of a variable, class property, or other operation, such as a function call.
Constructors and destructors
Neither of these functions implicitly calls the base class's response function, which is not the same as the constructor mechanism of Java. To achieve such an effect, the execution must be displayed. An exception cannot be thrown in a destructor.
Abstract class: A class method declared as abstract cannot contain a concrete implementation, and an abstract class cannot be instantiated. Must be inherited before instantiating its subclasses. And the subclass's access control is the same as the abstract class, or more relaxed. An abstract class contains at least one abstract method.
Interface
Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods.
All methods defined must be public and the method is empty
Constants can be defined, but no attributes
The implementation of the interface (implements) must implement all methods, and multiple interfaces can be implemented (note that the method cannot have duplicate names).
Interfaces can be inherited by additional interfaces (extends)
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"; } ?> |
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 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/629254.html www.bkjia.com true http://www.bkjia.com/PHPjc/629254.html techarticle a simple reference to the PHP object-oriented concepts and examples of the article, the need for a friend can simply refer to. Keywords and special variable new,class,extends. These three, we all know. ...