php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53765 $this variable doesn't get set using static reference
Submitted: 2011-01-17 11:51 UTC Modified: 2013-02-18 00:34 UTC
From: jon dot skarpeteig at gmail dot com Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.3.5 OS:
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
30 + 16 = ?
Subscribe to this entry?

 
 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-17 11:56 UTC] 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 18:23 UTC] cataphract@php.net
-Status: Open +Status: Feedback
 [2011-01-17 18:23 UTC] cataphract@php.net
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?
 [2013-02-18 00:34 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 18 02:01:27 2024 UTC