Edit report at http://bugs.php.net/bug.php?id=53765&edit=1

 ID:                 53765
 Updated by:         [email protected]
 Reported by:        jon dot skarpeteig at gmail dot com
 Summary:            $this variable doesn't get set using static
                     reference
-Status:             Open
+Status:             Feedback
 Type:               Bug
 Package:            Scripting Engine problem
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N

 New Comment:

I can't reproduce with this:



<?php

abstract class singleton {

        protected static $instance = null;

        final public static function getInstance() {

                if(null !== static::$instance)

                        return static::$instance;

                static::$instance = new static();

                return static::$instance;

        }

}

class myclass extends singleton {

        protected $var='foo';

        public function bar() { echo $this->var; }

}

$myclass = myclass::getInstance();

$myclass->bar();



echoes "foo". Is this the correct script?


Previous Comments:
------------------------------------------------------------------------
[2011-01-17 11:56:06] jon dot skarpeteig at gmail dot com

* correction:



abstract class singleton

{

protected static $instance = null;

final public static function getInstance()

        {

                if(null !== static::$instance){

                        return static::$instance;

                }

                static::$instance = new static();

                return static::$instance;

        }

}



and:



var_dump($this); - not echo

------------------------------------------------------------------------
[2011-01-17 11:51:16] jon dot skarpeteig at gmail dot com

Description:
------------
The test script output: 



Notice: Undefined variable: this



Instead $this is referenced as $instance



Attempting to do $this = $instance; returns:



Fatal error: Cannot re-assign $this

Test script:
---------------
abstract class singleton

{

final public static function getInstance()

        {

                if(null !== static::$instance){

                        return static::$instance;

                }

                static::$instance = new static();

                return static::$instance;

        }

}



class myclass extends singleton

{

protected $var='foo';

public function bar()

{

echo $this->var;

}

}



$myclass = myclass::getInstance();

$myclass->bar();

Expected result:
----------------
I expected $this to hold the same value as $instance

Actual result:
--------------
Notice: Undefined variable: this


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=53765&edit=1

Reply via email to