dongtang6681 2016-10-19 22: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 00: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部