note 51624 added to language.oop5.basic
| From: | rasmus at webb15 dot kontrollpanelen dot se | Date: | Tue, 05 Apr 2005 22:22:04 +0000 |
| Subject: | note 51624 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87543@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51624
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51624&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51624&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51624&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51624&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51624&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51624&report=yes
Search -- http://master.php.net/manage/user-notes.php