php how to achieve multi-state?

In PHP5, the variable type is uncertain, a variable can point to any type of numeric, string, object resources. We can not say in PHP5 polymorphism is variable.

 

We can only say that the type of cue position for PHP5, multi-state application method parameters.

Any sub-class object of a class are able to meet the current type as the type of prompt type requirements.

All classes implement this interface, the interface to meet all types of requirements type as a parameter prompt.

Simply put, a class has a parent class, and the identity of the interface has been implemented.

Achieve polymorphism through interface (recommended study: PHP Programming from entry to the master)

<?phpinterface User{ // User接口    public function  getName();    public function setName($_name);}class NormalUser implements User { // 实现接口的类.    private $name;    public function getName(){        return $this->name;    }    public function setName($_name){        $this->name = $_name;    }}class UserAdmin{ //操作.    public static function  ChangeUserName(User $_user,$_userName){        $_user->setName($_userName);    }}$normalUser = new NormalUser();UserAdmin::ChangeUserName($normalUser,"Tom");//这里传入的是 NormalUser的实例.echo $normalUser->getName();?>

Use a combination of analog interfaces and multiple inheritance

Multiple inheritance by combining simulation.

PHP does not support multiple inheritance, if we are to achieve code to the method of using multiple classes there any way to reuse it?

That's portfolio. In one class to another class will be set as an attribute.

The following example, simulates multiple inheritance.

Interface instance

Write an example of a conceptual. We designed an online sales system, the user part of the design as follows: users into, NormalUser, VipUser, InnerUser three. Buy discount calculated according to the requirements of different users, the price of the product. And requirements for the future expansion and maintenance of reserve space.

<?phpinterface User{    public function getName();    public function setName($_name);    public function getDiscount();}abstract class AbstractUser implements User{    private $name = "";    protected  $discount = 0;    protected  $grade = "";    function __construct($_name) {        $this->setName($_name);    }    function getName() {        return $this->name;    }    function setName($_name) {    $this->name = $_name;    }    function getDiscount() {        return $this->discount;    }    function getGrade() {        return $this->grade;    }}class NormalUser extends AbstractUser{    protected $discount = 1.0;    protected $grade = "Normal";}class VipUser extends AbstractUser{    protected $discount = 0.8;    protected $grade = "VipUser";}class InnerUser extends AbstractUser{    protected $discount = 0.7;    protected $grade = "InnerUser";}interface Product{    function getProductName();    function getProductPrice();}interface Book extends Product{    function getAuthor();}class BookOnline implements Book{    private $productName;    protected $productPrice;    protected $Author;    function __construct($_bookName) {        $this->productName = $_bookName;    }    function getProductName() {        return $this->productName;    }    function getProductPrice() {        $this->productPrice = 100;        return $this->productPrice;    }    public function getAuthor() {        $this->Author = "chenfei";        return $this->Author;    }}class Productsettle{    public static function finalPrice(User $_user, Product $_product,$ Number) {$ price = $ _user-> getDiscount () * $ _product-> getProductPrice () * $ number; return $ price;}} $ number = 10; $ book = new BookOnline ( "Design Patterns"); $ user = new NormalUser ( "tom"); $ price = Productsettle :: finalPrice ($ user, $ book, $ number);.. $ str = "Hello, Dear" $ user-> getName () "<br /> ";. $ str =" your level is <br /> "$ user-> getGrade ().." ";. $ str =" your discount is "$ user-> getDiscount ().." <br /> ";. $ str =" your price is "$ price;. echo $ str;?>"<br />";. $ Str = "Your level is" $ user-> getGrade () "<br />";... $ Str = "Your discount is" $ user-> getDiscount (. .) "<br />";. $ str = "your price is" $ price;. echo $ str;?>"<br />";. $ Str = "Your level is" $ user-> getGrade () "<br />";... $ Str = "Your discount is" $ user-> getDiscount (. .) "<br />";. $ str = "your price is" $ price;. echo $ str;?>

 

Guess you like

Origin www.cnblogs.com/68xi/p/11528063.html