PHP object-oriented concepts and examples

Source: Internet
Author: User

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;
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.