从对象内部访问同级对象

发布于 2024-11-16 18:59:48 字数 980 浏览 0 评论 0原文

我在表述标题中的问题时遇到了一些问题,但我希望您能给我一些示例代码。

基本上:

在面向对象的项目中,我想访问在“父”对象中定义的对象。 考虑这个片段:

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();
    }
}

正如您所看到的,“子”对象并不是真正的子对象,因为它们扩展了“父”类,而只是在对象内部实例化的对象。

有没有什么“哦,该死的,太酷了”的方法可以做到这一点,还是我必须将对象传递给对方 比如:

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

或者使用全局对象,在类之外实例化对象:

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

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

我希望你能理解我的意思;)

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 ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ζ澈沫 2024-11-23 18:59:48

我想你可能想使用 单例模式 (在 PHP5 中创建单例设计模式)。

尽管您可能还想对设计模式进行更多研究,具体来说你可能想看看PHP 中是否存在具有数据库访问权限的单例用例?

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?

无人接听 2024-11-23 18:59:48

我会将父对象交给子对象,也许不是通过构造函数,而是通过设置器。如果设置了父对象,那么我们就可以访问它的子对象:

/* 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);

我还会为对象使用一些子对象变量,然后您可以迭代对象的兄弟姐妹,引用它们的父对象。

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;
}

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;
}
相思碎 2024-11-23 18:59:48

这些对象是否作为相同类型、相同类或构成更大对象的不同对象相关?

  1. 相同类型,操作系统中嵌套文件夹和文件的示例

  2. 不同类型,组成一个更大的项目,例如由车轮、电机等组成的汽车类.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文