doufu6130
2011-06-22 13:48 阅读 34
已采纳

从对象内部访问同级对象

I had some problem formulating the question in the title, but i hope you get me with a bit of example code.

Basically:

In a objectoriented project i want to access an object that is defined in the "parent" object. consider this snippet:

class Bar
{
    public $var;
    public function Bar()
    {
        $this->var = 'value';
    }
}

class Bogus
{
    public function Bogus()
    {
        //Here i want to access the methods and vars of obj1 in the "parent" object
    }
}
class Foo
{
    public $obj1,$obj2;

    function Foo()
    {
        $this->obj1 = new Bar();
        $this->obj2 = new Bogus();
    }
}

as you can see the "child" object are not really childs in the sense that they extend the "parent" class but only objects instanciated inside an object.

is there any "oh damn thats cool" kinda way to do this or do i have to pass the objects to eachother like:

$this->obj2 = new Bogus($this->obj1);

or make use of global object, instanciating objects outside the class:

global $bar,$bogus;
class Foo
{
    public $obj1,$obj2;

    function Foo()
    {
        global $bar,$bogus;
        $this->obj1 = $bar   = new Bar();
        $this->obj2 = $bogus = new Bogus();
    }
}

I hope you can understand what im getting at ;)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dstjh46606 dstjh46606 2011-06-22 14:03

    I think you might want to use a Singleton Pattern (Creating the Singleton design pattern in PHP5).

    Although you also might want to do a little more research on design patterns in general, specifically you might want to take a look at Is there a use-case for singletons with database access in PHP?

    点赞 评论 复制链接分享
  • douze1332 douze1332 2011-06-22 14:02

    I would give the parent object to the children, maybe not by constructor, but a setter. And if the parent is set, then we are able to access its children:

    /* in Bogus ... */
    
    public function setParent($parent)
    {
        $this->parent = $parent;
    }
    
    public function getSiblingVar()
    {
        if (!empty($this->parent->obj1))
        {
            return $this->parent->obj1->var;
        }
        return false;
    }
    
    /* in Foo ... */
    
    $this->obj1 = new Bar();
    $this->obj2 = new Bogus();
    $this->obj2->setParent($this);
    

    I would also use some children object variable for the objects, then you can iterate an objects siblings, referencing their parent.

    public function getSiblingVar($sibling_id,$var_name)
    {
        if (!empty($this->parent->children[$sibling_id]))
        {
            return $this->parent->children[$sibling_id]->getVar($var_name);
        }
        return false;
    }
    
    点赞 评论 复制链接分享
  • doutui9606 doutui9606 2011-06-22 15:24

    Are those objects related as same type, same class, or different objects that compose a bigger object ?

    1. Same Type, example nested folders and files in your O.S.

    2. Different Type, compose a bigger item, like class Car composed by Wheels, Motor, etc.

    点赞 评论 复制链接分享

相关推荐