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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 16:01:28 2024 UTC