微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php中的类替换

有什么方法可以使它工作吗?见例子:
class car {
        function __construct($type){
                switch ($type) {
                        case 'big' : return new big_car();
                        case 'small' : return new small_car();
                }
        }
        function whatisit () {
                echo "this is car ;( \n";
        }
}

class big_car extends car {
        function __construct(){}
        function whatisit () {
                echo "this is big car ;) \n";
        }
}

class small_car extends car {
        function __construct(){}
        function whatisit () {
                echo "this is small car ;) \n";
        }
}

所以目标是以这种方式使用它:

$mycar = new car('big');
$mycar->whatisit(); // i want it to say that it's big

我非常想说它的方法不好而且它不能以这种方式工作但是也许有一个技巧?

PS ::我知道我可以使用特殊的静态方法,但……

你需要一家汽车厂来制造新车;这不是JavaScript

原文地址:https://www.jb51.cc/php/137553.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐