doufu6130 2011-06-22 13:48
浏览 39
已采纳

从对象内部访问同级对象

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

查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法