当我们像这样使用两个对象运算符标记时,对象结构会是什么 -> ->?

发布于 2024-09-06 23:20:07 字数 432 浏览 5 评论 0原文

我见过这样的事情:

echo ($hello->somethingA->somethingB);

这是什么意思?

我会尽力使我的问题更清楚:

当我们有 $domain->something; (我们正在访问 $domain OBJECT 的某些 PROPERTY。精确?

当我们有 $domain-> ;something->run(); 我们正在告诉 $DOMAIN OBJECT 的 some PROPERTY 来访问 run() 方法,

那么我们用: echo ($hello->somethingA. >somethingB); ?访问某些属性是否有意义?

- MEM

I've seen something like this:

echo ($hello->somethingA->somethingB);

What does this mean?

I will try to make my question more clear:

When we have $domain->something; (we are accessing something PROPERTY of $domain OBJECT. precise?

When we have $domain->something->run(); we are telling our something PROPERTY of $DOMAIN OBJECT to access run() METHOD. precise?

So what are we telling with: echo ($hello->somethingA->somethingB); ? Accessing some properties property? Does this makes sense?

Thanks in advance,
MEM

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

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

发布评论

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

评论(3

讽刺将军 2024-09-13 23:20:07

是的,您正在访问财产的财产。显然,$hello 是一个具有名为 $somethingA 属性的对象。该属性是一个具有名为 $somethingB 的属性的对象。 $somethingB 显然是一个字符串或其他可以回显的类型。

Yes, you are accessing a property's property. Apparently, $hello is an object with a property named $somethingA. That property is an object that has a property named $somethingB. $somethingB is apparently a string or some other type that can be echoed out.

苏大泽ㄣ 2024-09-13 23:20:07

+1 给斯科特桑德斯,这里有一个例子来说明它:

class Hello 
{
  /**
   * @var SomethingA
   */
  public $somethingA;
}

class SomethingA
{
  /**
   * @var, don't know what type
   */
  public $somethingB;
}

$hello = new Hello();
$hello->somethingA = new SomethingA();
$hello->somethingA->somethingB = new stdClass();

var_dump($hello);

+1 to Scott Saunders, here's an example to illustrate it:

class Hello 
{
  /**
   * @var SomethingA
   */
  public $somethingA;
}

class SomethingA
{
  /**
   * @var, don't know what type
   */
  public $somethingB;
}

$hello = new Hello();
$hello->somethingA = new SomethingA();
$hello->somethingA->somethingB = new stdClass();

var_dump($hello);
你不是我要的菜∠ 2024-09-13 23:20:07

其他答案中尚未探讨此问题的另一个方面。其他答案是您正在访问属性,而不是方法。但是 PHP5 的对象可以用于链接方法,也许这就是您所指的。

相反,

$obj = new Object();
$obj->setId('1');
$obj->setName('name');
$obj->setAge('24');

您可以这样做:

$obj = new Object();
$obj->setId('1')->setName('name')->setAge('24');

在每个方法中,返回 $this 就可以了。有关更多详细信息和灵感,请参阅本文:

http:// /www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html

There is another facet to this that hasn't been explored in the other answers. The other answers you are accessing properties, not methods. But PHP5's objects can be made to chain methods, and perhaps that's what you're referring to.

Instead of this:

$obj = new Object();
$obj->setId('1');
$obj->setName('name');
$obj->setAge('24');

You can do this:

$obj = new Object();
$obj->setId('1')->setName('name')->setAge('24');

In each method, return $this and you're golden. See this article for more details and inspiration:

http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html

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