Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a class with same name as built-in function?

Tags:

php

php-5.3

I would like to define a class named "List" like this:

class List
{
}

PHP gives the following error at the file of the class definition: "Parse error: syntax error, unexpected T_LIST, expecting T_STRING"

Apparently there is a php built-in function named "list()" that the parser is reading here instead of my class definition even though the line starts with the keyword class

Since I don't use the built-in function anywhere in my project I would like to "remove/disable" it, so I can use my class named "List".

Is this possible in php and how?

like image 704
Bazzz Avatar asked Feb 02 '26 03:02

Bazzz


2 Answers

list is a reserved word as it is a language construct (not actually a function), so no, you cannot disable it. Try being more specific with your class name, e.g., ObjectList or AbstractList.

like image 102
daiscog Avatar answered Feb 04 '26 16:02

daiscog


The class name can be any valid label which is a not a PHP reserved word.

List of PHP reserved words says list is one of those words.

like image 38
N.B. Avatar answered Feb 04 '26 16:02

N.B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!