public function add($child){
return $this->children[]=$child;
}
Btw, this is an excerpt from PHP in Action by Dagfinn Reiersol. According to the book, this returns $child, but shouldn’t it return true in case of successful assignment and false otherwise?
Thanks in advance
It returns
$child. This is because$childis first added to the array$this->children[]. Then, the result of this assignment is returned.Essentially, it is shorthand for:
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, with3being stored in$band then$a. Also, here is a note on the page I provided a link to:More information: http://en.wikipedia.org/wiki/Operator_associativity