views:

51

answers:

3

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

+4  A: 

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.

Scott Saunders
True. I'm starting to getting it know I believe. I will look back to the code, and comment it out A LOT. ;) Thanks.
MEM
+2  A: 

+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);
Bill Karwin
Wow! :) I know I'm dealing with an object that is "inside" another object. But actually, this "inside" as a meaning, and I believe is like so we code it? (does this make sense?)We have a class Hello with a property somethingA;We have another class SomethingA that as property, somethingB;(that fact that we have a property and a class name with the same name, can lead either to confusion or enlightenment. )We instantiate Hello class.We tell hello object property somethingA to have an instance of SomethingA class.We access a property of class SomethingA trough Hello class... ?Thanks
MEM
This is just an example. I do recommend choosing variable names that describe the purpose of the variable, not just its object type.
Bill Karwin
I do. Always. :) Don't use the type on variables on their name, but I'm quitePreciseEvenIfLongVariablesNeedsToBeMade :)
MEM
A: 

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

Nathan Loding
Method chaining is important because there is a severe shortage of semicolons in the world. Code sustainably! ;-)
Bill Karwin
Yes, now that I understand the sense of -> -> is indeed very nice possibilities those that may be open, either for object properties or object methods. I ways anyway, referring to properties. To be more precise, I'm using simpleXML interation capabilities, and if you var_dump that, you will see: a simpleXMLElement inside another simpleXMLElement, and each child node, seems to be a property. But please, correct me if I'm wrong. :)Thank you so much for your time guys.MEM
MEM
[SIC] - was and not ways. "I was, anyway, referring to properties".
MEM