php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52110 get_defined_vars is inconsistent with $this
Submitted: 2010-06-17 16:40 UTC Modified: 2010-06-17 17:05 UTC
From: jbwdevries at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.2 OS: Windows XP [Version 5.1.2600]
Private report: No CVE-ID: None
 [2010-06-17 16:40 UTC] jbwdevries at gmail dot com
Description:
------------
get_defined_vars() only contains the $this variable entry when $this is assigned or returned. In all other cases (no use, gettting or setting of property, callign a method) the $this entry is missing from the list.

Test script:
---------------
// Full example is at http://pastebin.com/FDJzCTRG

class Test
{
 	//...
 	public function testNoThis() {
		echo __METHOD__ . PHP_EOL;
		var_dump(array_keys(get_defined_vars()));
	}
 	//...
}

$t = new Test();
$t->testNoThis();


Expected result:
----------------
Test::testNoThis
array(1) {
  [0]=>
  string(4) "this"
}
Test::testThisGet
array(1) {
  [0]=>
  string(4) "this"
}
Test::testThisSet
array(1) {
  [0]=>
  string(4) "this"
}
Test::testThisCall
array(1) {
  [0]=>
  string(4) "this"
}
Test::testThisUse
array(1) {
  [0]=>
  string(4) "this"
}


Actual result:
--------------
Test::testNoThis
array(0) {
}
Test::testThisGet
array(0) {
}
Test::testThisSet
array(0) {
}
Test::testThisCall
array(0) {
}
Test::testThisUse
array(1) {
  [0]=>
  string(4) "this"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-17 17:05 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-06-17 17:05 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

For performance reasons $this and super-globals are only populated when they are needed. If the aren't needed they don't exist.
 [2010-06-17 17:10 UTC] jbwdevries at gmail dot com
I understand your reasoning; however, shouldn't you assume from the use of get_defined_vars() that $this is needed?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 11:01:28 2024 UTC