Making a method called "list" or other "language constructs" names.

php.internals

Urbanose

19 years ago
Hello there :] I wanted to build a method called "list". The problem is that once I do that, I get a "PHP Parse error: syntax error, unexpected T_LIST, expecting T_STRING" error, because it's a "language construct". Here's the code that leads me to this error : <?php class Foo { function __construct () { } function list () { echo "Hello"; } } $bar = new Foo (); $bar->list(); ?> So I made it working with a "__call" magic method, like this : <?php class Foo { function __construct () { } function __call ($name, $arguments) { echo "Hello"; } } $bar = new Foo (); $bar->list(); ?> And it works. So there's my question : why can't we make methods with the same name as those used by "language constructs" ? I don't see the point. Even less if you can make it work with the __call magic method. Does someone have an explanation ? Is there a way to make this possible for future versions of PHP, so that the classes namespace is really independent ? Don't tell me to use another name. Writting code like : $books->list('all'); or : class Books extends Controller { function list () { # Instructions to list all books } } makes things so much easier to read and self explanatory than putting, for example, a "_" in front of it (_list). Thanks :] Urbanose

Antony Dovgal

19 years ago
On 12/04/2006 05:22 PM, Urbanose wrote:
> And it works. So there's my question : why can't we make methods with > the same name as those used by "language constructs" ? I don't see > the point.
http://www.php.net/manual/en/reserved.php The point is that these names are reserved by the parser and cannot be used for other purposes.
-- Wbr, Antony Dovgal