# PHP5, objects not passed by reference?!?

- Thread: https://externals.io/message/16354
- Original archive: https://news-web.php.net/php.internals/16354
- Started by Jean-Francois Levesque on 2005-06-02T13:05:24+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #16354 — Jean-Francois Levesque — 2005-06-02T13:05:24+00:00

URL: https://externals.io/message/16354#16354

Hi!

In PHP5, the objects are supposed to be passed by reference, right?  I 
try to use a singleton pattern but it's not working.  I just compile the 
lastest php version (5.0.4) and I still have the problem.

My code :

<?php
class Example
{
    // Hold an instance of the class
    private static $instance;
    public $testvar = 'init';

    // The singleton method
    public static function singleton()
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }
    // Prevent users to clone the instance
    public function __clone()
    {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
}

$test = Example::singleton();
$test2 = Example::singleton();

$test->testvar = 1;
$test2->testvar = 2;

echo $test->testvar." ; ".$test2->testvar;

?>


Output :

Fatal error: Clone is not allowed. in 
/usr/home/usagers/linux/step_html/stationnement/test.php on line 21


Phpinfo : http://step.polymtl.ca/~linux/stationnement/phpinfo.php

Should I set something in the php.ini?  What can I do?

Thanks for your help!

Jean-Francois

---

## #16356 — Antony Dovgal — 2005-06-02T14:24:04+00:00

URL: https://externals.io/message/16354#16356 · In reply to: #16354

On Thu, 02 Jun 2005 09:05:24 -0400
Jean-Francois Levesque <linux@step.polymtl.ca> wrote:

> Hi!
> 
> In PHP5, the objects are supposed to be passed by reference, right?  I 
> try to use a singleton pattern but it's not working.  I just compile the 
> lastest php version (5.0.4) and I still have the problem.
> 
> My code :
> 
> <?php
> class Example
> {
>     // Hold an instance of the class
>     private static $instance;
>     public $testvar = 'init';
> 
>     // The singleton method
>     public static function singleton()
>     {
>         if (!isset(self::$instance)) {
>             $c = __CLASS__;
>             self::$instance = new $c;
>         }
> 
>         return self::$instance;
>     }
>     // Prevent users to clone the instance
>     public function __clone()
>     {
>         trigger_error('Clone is not allowed.', E_USER_ERROR);
>     }
> }
> 
> $test = Example::singleton();
> $test2 = Example::singleton();
> 
> $test->testvar = 1;
> $test2->testvar = 2;
> 
> echo $test->testvar." ; ".$test2->testvar;
> 
> ?>
> 
> 
> Output :
> 
> Fatal error: Clone is not allowed. in 
> /usr/home/usagers/linux/step_html/stationnement/test.php on line 21
> 
> 
> Phpinfo : http://step.polymtl.ca/~linux/stationnement/phpinfo.php
> 
> Should I set something in the php.ini?  What can I do?

I get "2 ; 2" and no error with latest snapshots of 5.0.5-dev and HEAD.
Check if you have zend.ze1_compatibility_mode Off.

-- 
Wbr, 
Antony Dovgal aka tony2001
antony@zend.com
