【问题标题】:PHP : Function argument must be an Object with dynamic class namePHP:函数参数必须是具有动态类名的对象
【发布时间】:2013-02-25 10:49:56
【问题描述】:

所以我是面向对象编程领域的新手,我目前正面临这个问题(代码中描述了一切):

<?php
    class MyClass {
        // Nothing important here
    }

    class MyAnotherClass {
        protected $className;

        public function __construct($className){
            $this->className = $className;
        }
        public function problematicFunction({$this->className} $object){
            // So, here I obligatorily want an $object of
            // dynamic type/class "$this->className"
            // but it don't works like this...
        }
    }

    $object = new MyClass;
    $another_object = new MyAnotherClass('MyClass');

    $another_object->problematicFunction($object);
?>

谁能帮帮我?

谢谢,Maxime(来自法国:对不起我的英语)

【问题讨论】:

  • 你不能使用{$this->className},检查$object with instanceof in questionsFunction
  • 你想用这个实现什么?自动加载器?

标签: php class function dynamic arguments


【解决方案1】:

你需要的是

public function problematicFunction($object) {
    if ($object instanceof $this->className) {
        // Do your stuff
    } else {
        throw new InvalidArgumentException("YOur error Message");
    }
}

【讨论】:

    【解决方案2】:

    这样试试

    class MyClass {
        // Nothing important here
        public function test(){
            echo 'Test MyClass';
        }
    }
    
    class MyAnotherClass {
        protected $className;
    
        public function __construct($className){
            $this->className = $className;
        }
        public function problematicFunction($object){
            if($object instanceof $this->className)
            {
                $object->test();
            }
        }
    }
    
    $object = new MyClass;
    $another_object = new MyAnotherClass('MyClass');
    
    $another_object->problematicFunction($object);
    

    【讨论】:

      【解决方案3】:

      这叫做type hinting,只是不支持你想做的事情。

      如果所有这些动态类名都有一些共同点(例如,它们是某些功能的不同实现),您可能想要定义一个基(可能是抽象)类或接口,并使用该共同祖先作为类型提示:

      <?php
      
      interface iDatabase{
          public function __contruct($url, $username, $password);
          public function execute($sql, $params);
          public function close();
      }
      
      class MyClass implements iDatabase{
          public function __contruct($url, $username, $password){
          }
      
          public function execute($sql, $params){
          }
      
          public function close(){
          }
      }
      
      class MyAnotherClass {
          protected $className;
      
          public function __construct($className){
              $this->className = $className;
          }
          public function problematicFunction(iDatabase $object){
          }
      }
      

      否则,只需将支票移至 problematicFunction() 正文内,正如其他答案所解释的那样。

      【讨论】:

        猜你喜欢
        • 2014-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-18
        • 1970-01-01
        • 2015-05-21
        • 1970-01-01
        相关资源
        最近更新 更多