php oop扩展问题

发布于 2024-09-26 07:39:14 字数 659 浏览 3 评论 0原文

我有以下课程。

class base {

    private function __construct(){
        //check if foo method exists 
        //$this->foo(); // if it exists
    }

    public static function singleton(){
        if(!isset(self::$singleton)){
            self::$singleton = new base();
        }
        return self::$singleton;
    }
}

class sub extends base{
    public function __construct() {
        parent::singleton();
    }

    public function foo(){

    }
}

然后像这样初始化它,

$test = new sub();

我的问题是我想检查 base __construct 子组件是否有 foo 方法。 但好像没有这个方法。

有人可以告诉我我哪里出了问题吗?

i have the following classes.

class base {

    private function __construct(){
        //check if foo method exists 
        //$this->foo(); // if it exists
    }

    public static function singleton(){
        if(!isset(self::$singleton)){
            self::$singleton = new base();
        }
        return self::$singleton;
    }
}

class sub extends base{
    public function __construct() {
        parent::singleton();
    }

    public function foo(){

    }
}

then init it like so

$test = new sub();

my problem is that I want to check on base __construct if the sub has a foo method.
but it doesn't seem to have this method.

can someone tell me where have I gone wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-10-03 07:39:15

虽然您从 sub 类调用 parent::singleton() ,但是 singleton() 仍然为您创建 base 的实例code> 类(因为您执行了 new base()),它没有您的 foo() 方法。

一般来说,您不应该使用/调用基类中未定义的任何方法。因为它使您的代码不干净:如果您将实现另一个扩展您的 base 类的类,但忘记实现 foo() 方法怎么办?您可能很快就会遇到致命错误...

如果您确定,此 foo() 方法将始终由任何子类实现 - 您可以在基类中定义为抽象方法 -那么任何子类都将被迫实现它。或者至少作为同一基类中的空方法......这样您的代码将变得干净且结构化。

Although you call parent::singleton() from sub class, but the singleton() still creates you instance of base class (because you do new base()), which does not have your foo() method.

In general you shouldn't use/call any methods from base class, which aren't define in it. Because it makes your code not clean: what if you will implement some another class which extends your base class, but forgets to implement the foo() method? You can end up with fatal errors quite fast...

If you are sure, that this foo() method will be always implemented by any child class - you can define in as abstract method in base class - then any child class will be forced to implement it. Or at least as an empty method in the same base class... This way your code will be clean and structured.

还在原地等你 2024-10-03 07:39:15

方法 foo 将不存在,因为正在创建的单例是 base 的实例。正在发生的事情是这样的:

  1. 创建 sub 的实例
  2. sub 的构造函数获取单例实例。
  3. singleton() 创建 base 的实例
  4. base 构造函数检查 base 类是否包含名为 foo 的方法。事实并非如此。

编辑

我有一个url,例如site.com/sub/foo,所以类sub被调用,我希望基类检查foo是否不存在,然后我将破坏代码...foo可以是任何东西,只要它作为子类的方法存在

在这种情况下,单例模式是多余的。相反,您可以从实现所需前端控制器模式的基本工厂类开始:

class factory {
  static public function go($path) {
    list($type, $action) = explode('/', $path);
    if (class_exists($type) &&   // Does the requested type/class exist?
        is_subclass_of($type, 'factory') &&  // Basic security. Make sure URL requests can't create an instance of *any* PHP class
        method_exists($type, $action))  // Check requested method/action
    {
      $o = new $type;
      $o->$action();
    }
  }
}

class sub extends factory {
  public function foo() {
    echo('To foo or not to foo that is the question.');
  }
}

factory::go('sub/foo');

The method foo will not exist as the singleton being creating is an instance of base. Here's what's happening:

  1. Create an instance of sub
  2. Constructor of sub gets the singleton instance.
  3. singleton() creates an instance of base
  4. base constructor checks if the base class contains a method named foo. It does not.

Edit

i got a url e.g. site.com/sub/foo so the class sub is called and i want the base to check if foo does not exist then i will break the code... foo can be anything as long as it exists as a method on sub class

In this case the singleton pattern is superfluous. Instead, you can start with a basic factory class that implements the desired front controller pattern:

class factory {
  static public function go($path) {
    list($type, $action) = explode('/', $path);
    if (class_exists($type) &&   // Does the requested type/class exist?
        is_subclass_of($type, 'factory') &&  // Basic security. Make sure URL requests can't create an instance of *any* PHP class
        method_exists($type, $action))  // Check requested method/action
    {
      $o = new $type;
      $o->$action();
    }
  }
}

class sub extends factory {
  public function foo() {
    echo('To foo or not to foo that is the question.');
  }
}

factory::go('sub/foo');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文