It returns $child
. This is because $child
is first added to the array $this->children[]
. Then, the result of this assignment is returned.
Essentially, it is shorthand for:
public function add($child){
$this->children[]=$child;
return $child;
}
This type of shortcut works because, in PHP, assignment is "right-associative": http://www.php.net/manual/en/language.operators.precedence.php
This means that $a = ($b = 3)
is actually evaluated from right-to-left, with 3
being stored in $b
and then $a
. Also, here is a note on the page I provided a link to:
Although = has a lower precedence than
most other operators, PHP will still
allow expressions similar to the
following: if (!$a = foo())
, in which
case the return value of foo()
is put
into $a
.
More information: http://en.wikipedia.org/wiki/Operator_associativity