note 59675 added to language.references.return
| From: | warhogatwarhogdotnet at osu1 dot php dot net | Date: | Tue, 13 Dec 2005 08:04:47 +0000 |
| Subject: | note 59675 added to language.references.return | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100046@lists.php.net to get a copy of this message | ||
firstly a note on the post below: technically correct that -> "to get a reference to an
exsisting class and it's properties" should be "...to an existing object..."
in PHP5 it's senseless to return objects by reference.. let's say you have, as in the post
below, a class which should return a reference to its own instance (maybe it's been created
using the singleton pattern..), than it's no problem to simply return that variable holding the
instance.
In PHP5 variables holding objects are always references to a specific object, so when you return
(=copy) a variable "having" an object you in fact return a reference to that object.
Because of that behaviour, which is very common for many programming languages, especially those,
which are to be compiled (due to memory problems and so on), you have to use the clone-function
which was introduced in PHP5.
Here an example to underline what i mean:
<?php
class sample_singleton
{
protected static $instance = NULL;
private function __construct()
{ }
public static function create()
{ self::$instance = new sample_singleton(); }
public static function getInstance()
{ return self::$instance; }
public function yea()
{ echo "wuow"; }
}
sample_singleton::create();
// $singleton will be a reference to sample_singleton::$instance
$singleton = sample_singleton::getInstance();
$singleton->yea();
?>
Note some more (maybe) strange behaviour: although $instance is private you can have a reference
pointing on it. Maybe that does not seem strange to you in any way, but maybe you wondered as i did
: )
The code posted here was just a sample to illustrate my posting, for using the singleton pattern
please look it up in the manual. I just used this cause it was the simplest example which went in my
mind right know.
----
Manual Page -- http://www.php.net/manual/en/language.references.return.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59675
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59675&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59675&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59675&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59675&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59675&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59675&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59675&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59675&report=yes
Search -- http://master.php.net/manage/user-notes.php