通过调用方法获取其他预先存在的对象的引用的正确方法?

时间:2016-10-20 06:07:19

标签: php object methods reference

我试图用方法调用一个对象而且卡住了。

假设我有一个名为Entity的类,它用于伪结构类型变量。

对象Entity通常在其他Entity个实例之间具有多个关系。因此,我创建了一个类EntityModule来处理数据库保存,加载以及可能的其他方便的方法。

我想做的是......

让我想将Entity对象插入表中。 使用方法Entity->save(),我想从预先存在的EntityModule->saveEntity() obejct调用EntityModule方法。

我该怎么做? 我想,只是在一个类中使用其他实例化对象,它非常适合。

..对于业余爱好者而言非常困惑。以下是我的例子。

<?php
class Container
{
    public function __get($varName)
    {
        if(!isset($this->varName)) {
            // Can it be more simpler?
            switch($varName) {
                case 'PDO':
                    $this->PDO('dbdbdb');
                    break;
                case 'SessionStorage':
                    $this->SessionStorage($this->PDO);
                    break;
                default:
                    break;
            }
        }
    }

    // This is weird method.. can it be more simpler?
    public function __call($method, $args) {
        $n = count($args);
        if($n === 0) {
            $this->$method = new $method();
        } elseif($n === 1) {
            $this->$method = new $method($args[0]);
        } elseif($n === 2) {
            $this->$method = new $method($args[0], $args[1]);
        } elseif($n === 3) {
            $this->$method = new $method($args[0], $args[1], $args[2]);
        } elseif($n === 4) {
            $this->$method = new $method($args[0], $args[1], $args[2], $args[3]);
        } elseif($n === 5) {
            $this->$method = new $method($args[0], $args[1], $args[2], $args[3], $args[4]);
        } elseif($n === 6) {
            $this->$method = new $method($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
        } elseif($n === 7) {
            $this->$method = new $method($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6]);
        };
    }
}
?>

使用容器,我尝试动态实例化对象。

class Schema
{
    public $id;
    public $type;
    public $name;
    public $desc;

    public function __construct(int $id = false, $type = 0, $name = 'Temporal schema name', $desc = 'No description.')
    {
        $this->id = $id;
        $this->type = $type;
        $this->name = $name;
        $this->desc = $desc;
        return;
    }

    public function save()
    {
        if(!isset($ctn->SchemaModule)) {
            // This is another weird part.
            // Is this the right way to make a class? Call a random instance inside the class? Doesn't it wierd?????????? ???? ?.??  ??? mentalbreak.
            $ctn->SchemaModule->save($this);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

虽然我并不完全确定你要做的事情,但我认为你可能会有一些过于复杂的事情。或至少,使它过于抽象。

__call()魔术方法,以及其他&#34;重载&#34;应该非常谨慎地使用魔术方法(__get()&amp; __set())。由于它们使事情变得更加复杂,破坏了IDE支持并且通常是一个麻烦的解决方案。

在这种情况下,我认为在编写方法之后,或许在某种程度上重组课程之后,你最好得到最好的服务。这样你就可以明确地写出你的所有逻辑,而不是在PHP本身内部使用某种元语言 要么是这样,要么使用工厂模式来生成对象。其中明确声明了所需的方法,并在控制器中对这些对象进行操作(在容器上)。

说到第二部分,我不太清楚你从哪里得到$ctn对象,或者SchemaModule的关系。但是,从OOP中调用其他实例在OOP中并不罕见,至少只要这些实例和依赖关系被清楚标记即可。 大多数情况下,这些依赖项(并且应该)被提供给构造函数,然后保存在类本身中。这意味着控制器需要知道类的依赖关系,并在生成对象时提供它们。这再次与我上面的评论联系起来,关于在代码中清楚地拼写你的逻辑并避免整个&#34;元神对象&#34;情况。

乍一看,似乎工厂模式与这种模糊逻辑模式更相似,因为它也会根据某些输入动态生成对象。这里最大的区别是它创建的对象都是基于共享接口的类,因此您知道它们是兼容的并且可以相互交换。
总的来说,这就是我认为您在代码中遗漏的内容。

相关问题