欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP5中实现多态的两种方法实例分享

程序员文章站 2022-10-14 20:14:25
在php5中,变量的类型是不确定的,一个变量可以指向任何类型的数值、字符串、对象、资源等。我们无法说php5中多态的是变量。 我们只能说在php5中,多态应用在方法参数的...

在php5中,变量的类型是不确定的,一个变量可以指向任何类型的数值、字符串、对象、资源等。我们无法说php5中多态的是变量。

我们只能说在php5中,多态应用在方法参数的类型提示位置。
一个类的任何子类对象都可以满足以当前类型作为类型提示的类型要求。
所有实现这个接口的类,都可以满足以接口类型作为类型提示的方法参数要求。
简单的说,一个类拥有其父类、和已实现接口的身份。

通过实现接口实现多态

复制代码 代码如下:

<?php
interface 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();
?>

使用接口与组合模拟多继承

通过组合模拟多重继承。

在php中不支持多重继承,如果我们向使用多个类的方法而实现代码重用有什么办法么?

那就是组合。在一个类中去将另外一个类设置成属性。

下面的例子,模拟了多重继承。

接口实例

写一个概念性的例子。 我们设计一个在线销售系统,用户部分设计如下: 将用户分为,normaluser, vipuser, inneruser 三种。要求根据用户的不同折扣计算用户购买产品的价格。并要求为以后扩展和维护预留空间。

复制代码 代码如下:

<?php
interface 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("设计模式");
$user = new normaluser("tom");
$price = productsettle::finalprice($user, $book, $number);
$str = "您好,尊敬的" . $user->getname() . "<br />";
$str .= "您的级别是" . $user->getgrade() . "<br />";
$str .= "您的折扣是" . $user->getdiscount() . "<br />";
$str .= "您的价格是" . $price;
echo $str;
?>