php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38028 Enable possibility to iterate static or const member in class with foreach
Submitted: 2006-07-07 02:32 UTC Modified: 2006-07-07 08:56 UTC
From: rapsys at free dot fr Assigned: helly (profile)
Status: Not a bug Package: Feature/Change Request
PHP Version: 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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rapsys at free dot fr
New email:
PHP Version: OS:

 

 [2006-07-07 02:32 UTC] rapsys at free dot fr
Description:
------------
I noticed a limitation in (at least) latest version of php.

It's impossible to iterate the const or static member of a 
class.

The only possible (but REALY too hard) whould be to use the 
reflector propriety.

I don't know if it's a feature to add or a missing 
explanation in documentation.

But if it's not supported at all, it should be reported as 
a lack of feature in documentation and scheduled for php6 ?

Reproduce code:
---------------
<?php
//The class
class someclass {
	//The constant
	const someconst = 'somevalue';
	//The public static member
	public static $somepublicstatic = 'somevalue';
	//The protected static member
	protected static $someprotectedstatic = 'somevalue';
	//The private static member
	private static $someprivatestatic = 'somevalue';

	//A function to list static or const members
	public static function somefunction()
	{
		//Try to iterate const or static member
		foreach(self as $key => $value)
			echo "$key = $value\n";
	}
}

echo "static function list:\n";
someclass::somefunction();
echo "external list:\n";
foreach(someclass as $key => $value)
	echo "$key = $value\n";
?>

Expected result:
----------------
static function list:
someconst = somevalue
somepublicstatic = somevalue
someprotectedstatic = somevalue
someprivatestatic = somevalue
external list:
someconst = somevalue
somepublicstatic = somevalue


Actual result:
--------------
static function list:
PHP Notice:  Use of undefined constant self - 
assumed 'self' in /var/www/web/test.php on line 17

Notice: Use of undefined constant self - assumed 'self' 
in /var/www/web/test.php on line 17
PHP Warning:  Invalid argument supplied for foreach() 
in /var/www/web/test.php on line 17

Warning: Invalid argument supplied for foreach() 
in /var/www/web/test.php on line 17
external list:
PHP Notice:  Use of undefined constant someclass - 
assumed 'someclass' in /var/www/web/test.php on line 25

Notice: Use of undefined constant someclass - 
assumed 'someclass' in /var/www/web/test.php on line 25
PHP Warning:  Invalid argument supplied for foreach() 
in /var/www/web/test.php on line 25

Warning: Invalid argument supplied for foreach() 
in /var/www/web/test.php on line 25


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-07 08:56 UTC] helly@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

Use reflection api
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 09:01:28 2025 UTC