php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68148 $this is null inside include
Submitted: 2014-10-03 20:05 UTC Modified: 2014-10-05 21:44 UTC
From: pegasus at vaultwiki dot org Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: master-Git-2014-10-03 (Git) OS: Centos 6 64-bit
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: pegasus at vaultwiki dot org
New email:
PHP Version: OS:

 

 [2014-10-03 20:05 UTC] pegasus at vaultwiki dot org
Description:
------------
Per the description, $this is null inside an eval, even if the eval is in the object's scope.

Test script:
---------------
class Test
{
	public $hello = 'hi';

	public function test()
	{
		eval('echo $this->hello;');
	}
}

$o = new Test();
$o->test();

Expected result:
----------------
Expect to see printed

hi

Checking $this itself reveals NULL.

Actual result:
--------------
Nothing printed

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-03 20:15 UTC] pegasus at vaultwiki dot org
Okay, done some more testing, and I simplified the case too much here. The test I provided works, but the full case does not (works in 5.6.0). I will have to reinvestigate.
 [2014-10-03 20:24 UTC] pegasus at vaultwiki dot org
It has nothing to do with the eval, and has to do with $this not being available from an included script.

Create second file called hello.php:
####
<php?

echo $this->hello;
####

Use this test case from another file:
####
class Test
{
	public $hello = 'hi';

	public function testNow()
	{
		require('hello.php');
	}
}

$o = new Test();
$o->testNow();
####
 [2014-10-03 20:25 UTC] pegasus at vaultwiki dot org
-Summary: $this is null inside eval +Summary: $this is null inside include
 [2014-10-03 20:25 UTC] pegasus at vaultwiki dot org
updated title
 [2014-10-03 20:52 UTC] nikic@php.net
I can't reproduce this (with neither eval nor require).
 [2014-10-04 05:06 UTC] pegasus at vaultwiki dot org
Okay, yes hello.php does output 'hi' as expected, but the content is changed to
###
var_dump($this);
###
It outputs NULL, which is not expected, especially since var_dump($this->hello) results in string(2) "hi".

Now, to get hello.php to stop outputting 'hi' altogether, my hello.php is more complicated (and closer to the format of the real code that isn't working):
####
if (!function_exists('test_fn'))
{
	function test_fn(&$obj)
	{
		echo $obj->hello;
	}

	class NestedTest
	{
		public function doubleTest($obj)
		{
			echo $obj->hello;
		}
	}
}

test_fn($this); // outputs nothing, expected 'hi'

$o2 = new NestedTest();
$o2->doubleTest($this); // also outputs nothing, expected 'hi'
####
 [2014-10-05 21:44 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jun 16 23:01:33 2025 UTC