I'd like to create a function called "new" and a class called "case".
Can I do that in PHP?
What is a keyword. In PHP there are certain words that's reserved for a special use. We cannot use these words when naming our variables, constants, arrays, functions, interfaces and classes. These keywords have special meaning and is only to be used in special contexts.
A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ .
In PHP 7.2. 0RC2, Object is a reserved word.
No, you can't. Thank god.
Actually, while defining such a method results in a parse error, using it does not, which allows some kind of workarounds:
class A {
function __call($method, $args) {
if ($method == 'new') {
// ...
}
}
}
$a = new A;
$a->new(); // works!
A related feature request dating back to 2004 is still open.
As of PHP 7, it is now possible to name your methods using keywords that were restricted so far, thanks to the Context Sensitive Lexer:
class Foo {
public function new() {}
public function list() {}
public function foreach() {}
}
You still can't, however, name your class Case
I'm afraid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With