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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jon dot skarpeteig at gmail dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2022 The PHP Group
All rights reserved.
Last updated: Fri Oct 07 11:05:53 2022 UTC