父对象在php [英] Parent Object in php

查看:132
本文介绍了父对象在php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法遍历一个对象来获取父对象数据?
父对象我不是指父类,而是字面上的对象。
这里是一个在javascripty世界中的例子:-):

is there a way to traverse an object to get the parent object data? With "parent object" I don't mean the parent class, but literally object. Here an example, in a javascripty world :-) :

$parent->test = "hello world!";

$parent->child = new B();

这将是巨大的如果我可以访问子对象从父对象的所有数据:

It would be great If I could access all the data from parent in the child object:

class B{

//I know this doesn't exists, but it's what I wanted do do
    function B(){
        $this->parent = $this->parent();
        echo $this->parent->test; //it would ouput "hello world"
    }
}

我的解决方案是将父对象传递给子(作为参考)或使父父全局。
你有什么更好的解决方案吗?

For now my solution is to pass the parent object to the child (as a reference) or to make the parent global. Do you have any better solution?

谢谢!

推荐答案

无法调用

$parent->test = "hello world!";
$parent->child = new B();

并自动引用B中的$ parent。

and automatically have a reference to $parent in B.

通常,有四种方法来构建类:

Generally, there is four ways to structure your classes:

1。通过注入汇总父对象,例如

class B
{
     private $parent;

     public function __construct($parent) 
     {
         $this->parent = $parent;
     }

     public function setParent($parent) 
     {
         $this->parent = $parent;
     }

     public function accessParent() 
     {
        $this->parent->someMethodInParent();
     }
}

使用构造函数注入当对象必须有父当它创建。这是一个 has-a 关系,它创建了一个非常宽松的耦合。 B中没有硬编码的依赖项,因此您可以轻松地切换父实例,例如在UnitTesting时使用Mock。

Use constructor injection when the object has to have a parent when it's created. This is a has-a relationship and it creates a very loose coupling. There is no hardcoded dependencies in B, so you can easily swap out the Parent instance, for instance with a Mock when UnitTesting. Using Dependency Injection will make your code more maintainable.

在您的UseCase中,您可以将 $ parent 传递给B当创建B时:

In your UseCase, you'd pass $parent to B when creating B:

$parent->child = new B($parent);

2。使用组合

class B
{
     private $parent;

     public function __construct() 
     {
         $this->parent = new Parent;
     }

     public function accessParent() 
     {
        $this->parent->someMethodInParent();
     }
}

这也是一个关系,但将Parent类与B耦合。它也不是现有的Parent实例,而是一个新实例。从文字上来说,我认为有一个父母是由孩子创造的有点奇怪。使用这个,当依赖是一个不被认为存在于根类以外的类,但是它代表的整个东西的一部分。

This is also a has-a relationship, but couples the Parent class to B. It's also not an existing Parent instance, but a new instance. From the wording I find it somewhat odd to have a parent created by the child. Use this, when dependency is a class that is not considered to exist outside of the root class, but is part of the whole thing it represents.

对于你的UseCase是没有办法 $ parent-> child = new B(); 并且知道使用这种方法时父类是什么,除非$ parent是Singleton。如果是这样,你可以得到Singleton实例,例如。 Parent :: getInstance()来实现你想要的,但注意Singletons并不是每个人都喜欢的模式,例如。很难测试。

For your UseCase, there is no way of doing $parent->child = new B(); and know what parent is when using this approach, unless $parent is a Singleton. If so, you could get the Singleton instance, e.g. Parent::getInstance() to achieve what you want, but note that Singletons are not everyone's favorite pattern, e.g. hard to test.

3。使用继承

class B extends Parent 
{
    public function accessParent() 
    {
        $this->someMethodInParent();
    }
}

这样,您就可以创建 is-a 关系。来自Parent类的所有公共和受保护的方法和属性(但不是特定实例)将在B中可用,您可以通过 $ this B实例。

This way you create an is-a relationship. All public and protected methods and properties from the Parent class, (but not of a specific instance) will be available in B and you can access them via the $this keyword of the B instance.

对于你的UseCase,这种方法不工作,因为你不必有一个Parent的实例,但是B会封装Parent的一切

For your UseCase, this approach is not working, as you don't have to have an instance of Parent at all, but B would encapsulate everything of Parent when it's created

$b = new B;

4。使用全局关键字

class B extends Parent 
{
    private $parent;

    public function __construct() 
    {
        global $parent;
        $this->parent = $parent;
    }

    public function accessParent() 
    {
        $this->parent->someMethodInParent();
    }
}

全局关键字将全局变量导入当前范围。一般来说,应避免在OO上下文中使用全局关键字,但是使用上述其他三种方法中的一种,优选第一种。虽然它是一个语言功能,但它是皱眉的 - 虽然它是第一个最接近的事情,例如

The global keyword imports global variables into the current scope. In general, you should avoid using the global keyword in an OO context, but use one of the other three methods above, preferably the first one. While it's a language feature, it's frowned upon - although it is the next closest thing to the first one, e.g.

$parent->child = new B();

无论如何,希望有帮助。

Anyway, hope that helps.

这篇关于父对象在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆