从对象内部访问同级对象
我在表述标题中的问题时遇到了一些问题,但我希望您能给我一些示例代码。
基本上:
在面向对象的项目中,我想访问在“父”对象中定义的对象。 考虑这个片段:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你可能想使用 单例模式 (在 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?
我会将父对象交给子对象,也许不是通过构造函数,而是通过设置器。如果设置了父对象,那么我们就可以访问它的子对象:
我还会为对象使用一些子对象变量,然后您可以迭代对象的兄弟姐妹,引用它们的父对象。
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:
I would also use some children object variable for the objects, then you can iterate an objects siblings, referencing their parent.
这些对象是否作为相同类型、相同类或构成更大对象的不同对象相关?
相同类型,操作系统中嵌套文件夹和文件的示例
不同类型,组成一个更大的项目,例如由车轮、电机等组成的汽车类.
Are those objects related as same type, same class, or different objects that compose a bigger object ?
Same Type, example nested folders and files in your O.S.
Different Type, compose a bigger item, like class Car composed by Wheels, Motor, etc.