0

Possible duplicate of Reference - What does this symbol mean in PHP?

What does it mean when you have something like this $this->_view->id ?

It is within a class (obviously) and I understand the $this. I get how to use one -> to refer to a property or call a method. But what about when there are two -> in one thing?

The fuller code is:

$viewid = ($this->_view) ? $this->_view->id : null;

I'm guessing the overall gist is: Set $viewid to either (1) the value of $this->_view->id or (2) null, depending on whether $this->_view is TRUE or not. But I don't get the (1) bit.

Also, is it conventional to use an underscore (_view) to show a property or a method?

Thanks.

Community
  • 1
  • 1
Nick
  • 5,995
  • 12
  • 54
  • 78
  • 2
    The underscore usually denotes that a member variable or a method is private. Not everybody follows this however. – linepogl Feb 11 '12 at 08:26
  • http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – vascowhite Feb 11 '12 at 08:30
  • The Reference info on the symbol doesn't go into multiple uses of -> in one entity, @vascowhite, but thanks anyway :) – Nick Feb 11 '12 at 09:08

3 Answers3

4

This means that there is an object stored within an object.

$this->_view refers to an object called $_view within. So within $_view object $this->id would refer to its $id variable.

So calling $this->_view->id calls for variable $id in an object $_view that is stored in your current object (since you said its $this).

Detailed:

class firstClass {
    public $_view;
}
class secondClass{
    public $id=1;
}
$a=new firstClass();
$a->_view=new secondClass();
echo $a->_view->id; // prints 1
kingmaple
  • 4,200
  • 5
  • 32
  • 44
  • Umm... I'm still thinking here, @kristovaher. At the top of the class it says var $_view = null; Further down the code it has: if ($this->_view) {return $this->_view->getValue('name'); So in this case, getValue has to be a method, right? I'm confident your way works. But can you have $this=object->_view=property->getValue()=method? If so, id still can't be a method because there is no (), right? So could it be $this=object->_view=property->id=property ? Surely not. I'm still confused, I'm afraid. – Nick Feb 11 '12 at 08:47
  • 1
    At the top of the class it simply says that $_view is undefined at first. Look for when anything is assigned to $_view. Note that based on how complicated (or how badly) code is written, $_view may also be assigned dynamically someplace (such as through a Factory pattern), making debugging more difficult. – kingmaple Feb 11 '12 at 08:52
  • Yes, I see now. Declaring it with the var $.. = null; is a trap for young (read: noob) players, though! Thanks, again. – Nick Feb 11 '12 at 09:02
1

$viewid = ($this->_view) ? $this->_view->id : null;

It means if view object is available for current object, then set its id to $viewid, otherwise set it to null.

for example $this is current object of UserClass. $this->_view is object of view of UserClass and $this->_view->id is id of view object.

Generally, for private or protected member of class, we start with _

Gaurav
  • 28,447
  • 8
  • 50
  • 80
0

$id is attached to an object returned by the function function _view.

The underscore is a preference,

thenetimp
  • 9,487
  • 5
  • 29
  • 42
  • I think that kristovaher is right, @thenetimp. _view is an object, not a function/method! Thanks, though. – Nick Feb 11 '12 at 09:03