php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #47369 use of {self::$} in a string always triggers Notice: Undefined variable:
Submitted: 2009-02-12 13:26 UTC Modified: 2009-09-05 06:39 UTC
From: d4n at digitalanalogue dot org Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: CentOS 5
Private report: No CVE-ID: None
 [2009-02-12 13:26 UTC] d4n at digitalanalogue dot org
Description:
------------
Can you update the strings document to explain why you cannot use self::$var in any string, or if you can what php version it works in.

I do not know if this is a bug, or if it is my php version, but I can't understand why I can access the var and echo it out yet when I put it in a string using escaped methods it is ignored and throws a notice saying undefined when it clearly is!

Thanks

Dan

Reproduce code:
---------------
class user {
	
	private static $table = "foo";
	
	public function user() {
	
		//blah...
	
	}
	
	public static function test() {
	
		$str = "table = ". self::$table . "<br/>";
		echo $str;
		// Outputs: table = foo
		$str = "table = {self::$table} <br/>";
		echo $str;
		// Outputs: 
		// Notice: Undefined variable: table in /*** on line ***
		$str = '{self::$table}' . "<br/>";
		echo $str;
		// Outputs: table = {self::}
		$str = <<<EOD
		table = self::$table
EOD;
		/* 
		Outputs: 
		Notice: Undefined variable: table in /*** on line ***
		table = self::
		*/
		echo $str;	
	}
	
}

Expected result:
----------------
table = foo
table = foo
table = {self::$table} 
table = foo

Actual result:
--------------
table = foo 
Notice: Undefined variable: table in /*** on line ***
table = {self::} 
table = {self::$table} // correct!
Notice: Undefined variable: table in /*** on line ***
table = self::

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-16 15:17 UTC] smlerman at gmail dot com
If I remember correctly, the parser only looks for {$ to do variable interpolation, which means you can't use things like constants or static properties.
 [2009-09-05 06:39 UTC] svn@php.net
Automatic comment from SVN on behalf of torben
Revision: http://svn.php.net/viewvc/?view=revision&revision=288077
Log: Noted that the values of static class variables and class constants
may be accessed as the names of variables in the local scope, but that
single-curly-brace syntax will not work with function or method calls
or static class variables or class constants.
Addresses bug #47369.
 [2009-09-05 06:39 UTC] torben@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Apr 26 01:01:28 2025 UTC