Bug #53765 [Opn]: $this variable doesn't get set using static reference

From: Date: Mon, 17 Jan 2011 10:56:07 +0000
Subject: Bug #53765 [Opn]: $this variable doesn't get set using static reference
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-157949@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=53765&edit=1 ID: 53765 User updated by: jon dot skarpeteig at gmail dot com Reported by: jon dot skarpeteig at gmail dot com Summary: $this variable doesn't get set using static reference Status: Open Type: Bug Package: Scripting Engine problem PHP Version: 5.3.5 Block user comment: N Private report: N New Comment: * 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 Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#157949) next »