php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65305 $this keyword is treated like a variable under certain circumstances
Submitted: 2013-07-21 14:47 UTC Modified: 2013-07-21 15:07 UTC
From: ryan at skidmore dot pro Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.4.17 OS: Linux/Ubuntu
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: ryan at skidmore dot pro
New email:
PHP Version: OS:

 

 [2013-07-21 14:47 UTC] ryan at skidmore dot pro
Description:
------------
When capitalising the first letter of the $this keyword to $This and then 
assigning it to a variable (with appropiate class structures) like this: 
"$Variable = $This->Class1->Function1();" causes PHP to throw an error and not 
display the page, when $This is changed to $this, it works fine.

Test script:
---------------
This test script is fit into two parts, for two separate files.

** UNCOMMENT THE LINES TO REPRODUCE PROBLEM

** FILE 1 ** (The file to be run)

Class Class1
{
	
	Public $Class2Inst;
	
	Public function __construct()
	{
		
		include("phpbug2.php"); // ******* Change this to the path of the other file.
		
		$this->Class2Inst = new Class2;
		
		//$Variable1 = $This->Class2Inst->Func1(); // This produces error
		
		//$Variable2 = $this->Class2Inst->Func1(); // This does not produce error
		
		echo $Variable2;
		
	}
	
}

$Class1Inst = new Class1;


** FILE 2 ** The file that gets included

Class Class2
{
	
	Public function Func1()
	{
		
		return "Function 1 is being run";
		
	}
	
}


Expected result:
----------------
The expected result is that Func1 within the Class2 class is run.

Actual result:
--------------
The $This keyword is treated like a variable, instead of like a keyword.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-21 14:54 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2013-07-21 14:54 UTC] nikic@php.net
$this *is* a variable and variables in PHP are case-sensitive. Thus your code makes a method call on null, which results in a fatal error.
 [2013-07-21 15:03 UTC] ryan at skidmore dot pro
Surely it would be logical to treat $This exactly the same as $this though?
 [2013-07-21 15:05 UTC] nikic@php.net
Why would it be logical? PHP treats all variables case-sensitively, so it's not really obvious to me why $this should be any different in this regard.
 [2013-07-21 15:07 UTC] ryan at skidmore dot pro
$this is used universally as essentially a keyword that can be used to access 
something within the current class, and while it isn't technically a keyword - I 
think it should be treated like one in terms of capitalisation.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 28 06:03:33 2021 UTC