note 51624 deleted from language.oop5.basic by colder
| From: | colder@php.net | Date: | Sun, 19 Aug 2007 23:40:44 +0000 |
| Subject: | note 51624 deleted from language.oop5.basic by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-129610@lists.php.net to get a copy of this message | ||
Note Submitter: rasmus at nospaam dot flajm dot com
----
A very good (in my opinion) change in PHP5 is that objects are always passed by reference. In PHP4
they where passed by value, which cased a copy to be made for every call. (as long as you
didn't use "&" of cause)
This example illustrates pass-by-reference, as the name changes in the same object, passed around
two different ways:
<?php
class Mother {
private $obj = null;
public function setChild($o) {
$this->obj = $o;
}
public function getChild() {
return $this->obj;
}
}
class Child {
private $name = 'noname';
public function setName($string) {
$this->name = $string;
}
public function getName() {
return $this->name;
}
}
$mom = new Mother();
$frasse = new Child();
$frasse->setName('Johan');
$mom->setChild($frasse);
$frasse->setName('Frans');
print $frasse->getName() . "\n";
print $mom->getChild()->getName() . "\n";
?>
This will print:
Frans
Frans
In PHP4, this would print:
Frans
Johan