|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-01-17 11:51 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 13:00:01 2025 UTC |
* 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 echoI 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?