Php Getting Started Tutorial-Object-oriented

Source: Internet
Author: User
Tags abstract constructor

First case: when the subclass does not define the constructor, it inherits by default.

Case 2: If the subclass defines the constructor, it will not be inherited.

For 4.x, if the parent class exactly defines the function with the same name as the subclass, it will be treated as the constructor of the subclass:

The code is as follows: Copy code

Class
{
Function ()
    {
Echo "I am the constructor of A. <br> n ";
    }

Function B ()
    {
Echo "I am a regular function named B in class A. <br> n ";
Echo "I am not a constructor in A. <br> n ";
    }
}

Class B extends
{
Function C ()
    {
Echo "I am a regular function. <br> n ";
    }
}

// Php4 will call B ()
$ B = new B;

In php5, the above code calls A instead of B ()

PHP object-oriented: interface instances

Design an online sales system. The user part is as follows:

Users can be divided into NormalUser, VipUser, and InnerUser.

You must calculate the price of your purchased product based on your different discounts.

It is required to reserve space for future expansion and maintenance.

The User part first declares an interface User, which is the implementation of all users.

User. php

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

We have designed the product as follows.

Declare an interface Product and inherit the Book interface from the Product.

Books sold online are the BookOnline class that implements the Book interface.

Product. php

The code is as follows: Copy code

<?
/* Product-related classes .*/
Interface Product {// define the Product Interface
Public function getProductName ();
Public function getProductPrice ();
}

Interface Book extends Product {// book is a Product category
Public function getAuthor ();
}

Class BookOnline implements Book {// defines the book class.
Private $ productName; // product name
Private $ productPrice; // product price
Private $ author; // author
 
Public function _ construct ($ _ bookName ){
$ This-> productName =$ _ bookName;
// Put the initialization code here.
// Code associated with the database.
 }
 
Public function getProductName (){
Return $ this-> productName;
 }
 
Public function getProductPrice (){
// Read the price from the database here.
// Assume the price is 100 RMB.
$ This-& gt; productPrice = 100;
Return $ this-> productPrice;
 }
 
Public function getAuthor (){
// Obtain the value from the database.
Return $ this-> author;
 } 
}
?>

For settlement, we use an independent settlement class and a static method for calculation. 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 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

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.