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 = 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.