php多态

对php多态的理解:

/**
 * 定义接口
 */
interface Shape{
    public function draw();    
}
/**
 * 三角形
 */
class Triangle implements Shape{
    public function draw(){
        echo "This is Triangle";    
    }
}
/**
 * 矩形
 */
class Rectangle implements Shape{
    public function draw(){
        echo "This is Rectangle";    
    }
}
class TestPoly{
    public function drawNow($shap){
        $shap->draw();
    }
}
$test = new TestPoly();
$test->drawNow(new Triangle);
echo '<br />';
$test->drawNow(new Rectangle);
If the copyright belongs to the longfei, please indicate the source!!!
原文地址:https://www.cnblogs.com/longfeiPHP/p/5394225.html