dongtang6681 2016-10-20 06:07
浏览 59
已采纳

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

I'm trying to call an object using method and I got stuck.

Lets suppose that I have a class named Entity which used to pseudo-struct-type variable.

The object Entity is usually have multiple relation among other Entity instances. So, I make a class EntityModule to handle database save, load, and possibly other handy methods.

What I'm trying to do is..

Lets if I want to insert Entity object to the table. With a method Entity->save(), I want to call EntityModule->saveEntity() method from pre-exist EntityModule obejct.

How should I get this? I think, just using other instantiated object inside a class, its very weired.

.. Its very confusing for amateur.. Following is my example.

<?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]);
        };
    }
}
?>

Using the container, I try to instantiate object dynamically.

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条回答 默认 最新

  • dphs48626 2016-10-20 08:15
    关注

    While I'm not 100% sure of what you're trying to do, I do think that you might have overcomplicated things a bit. Or at least, made it overly abstract.

    The __call() magic method, as well as the other "overloading" magic methods (__get() & __set()), should be used very sparingly. As they make things a lot more complicated, breaks IDE-support and is generally a cumbersome solution.

    In this case I think you'd be best served with either writing out the methods, after perhaps restructuring the classes somewhat. So that you have all of your logic written out explicitly, instead of being some sort of meta-language inside PHP itself.
    Either that, or using a factory pattern to generate the objects. Which has the required methods explicitly stated, and doing the operations with these objects in the controller instead (of on the container).

    When it comes to the second part, I don't quite understand where you get the $ctn object from, or the relation of the SchemaModule in it. But, calling other instances from within methods is nothing unusual in OOP, at least not as long as those instances and dependencies are clearly marked.
    Most of the time these dependencies are (and should) be provided to the constructor, and then saved in the class itself. Meaning that the controller needs to know the dependencies of the classes, and provide them upon object generation. This again ties into the comment I made above, about spelling your logic out clearly in the code and avoiding the whole "meta God object" situation.

    At first glance it might seem that the factory pattern is more of the same of this fuzzy logic pattern, since it too generates objects dynamically depending upon some input. The big difference here is that the objects it creates are all based upon classes which share an interface, so that you know they are compatible and mutually-interchangable.
    This is what I think you're missing in your code, by and large.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法