Can I use PHP reserved names for my functions and classes?
Asked Answered
I

6

14

I'd like to create a function called "new" and a class called "case".

Can I do that in PHP?

Ivanna answered 14/3, 2011 at 12:21 Comment(7)
Why do you want to confuse yourself?Codification
Do yourself a favour and don't do this.Denary
I wanted to do this because I'm doing a lawyer website and I want to have some models named "case" and in them a function "new" which will create a new caseIvanna
No, now why do you want to assign names like these rather than more sensible function and class names?Bareilly
You can't, and with very good reason.Whitewood
Ok ok.. I get it.. I can't :)Ivanna
possible duplicate of Is it possible to "escape" a method name in PHP, to be able to have a method name that clashes with a reserved keyword?Vinegarroon
V
30

No, you can't. Thank god.

Vinegarroon answered 14/3, 2011 at 12:25 Comment(7)
It could be quite useful, for example look at rails controllersUnformed
@Alexcp: It could be quite useful if your goal is to mangle your codebase, confuse everyone's syntax highlighters and drive your fellow developers insane.Vinegarroon
@Alexcp: I wanted to do this in CodeIgniter, imitating my Rails controllers. I called my controller methods new_() and routed */new to my new_() methods: $route['([A-Za-z_.-]+)/(new)'] = "$1/new_";Nubilous
@Josef: I'm devastatingly sorry to hear that.Vinegarroon
@LightnessRacesinOrbit: Why? My URLs look nice and it should be easy for another programmer to see the correlation between the URL and the method without looking through the routing rules.Nubilous
@JosefOttosson: Because of everything I already said about using reserved keywords as function names. It's just evil. Your workaround is needless and messy!Vinegarroon
@LightnessRacesinOrbit: But if you read my code, that's exactly what I'm avoiding! And if your issue is that my method name is too similar in your eyes, just change it. "new" is a part of the route (a requirement I had to deal with), and CodeIgniter maps routes against methods by default. So I changed how CI maps routes, a piece of code you'll never have to see when writing your controllers, so that I do not have to call the method "new". My "workaround" is clean compared to persuading a whole organisation that they'll have to stop typing "/post/new", not to mention breaking URLs.Nubilous
Y
30

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.

Edit January 2016

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.

Yacketyyak answered 26/7, 2011 at 16:5 Comment(3)
We all know what the doc currently says, however discussions are always on, and not all people are as virulent as you are on this subject. The fact is that there is currently quite an inconsistency between having a parse error raised when a method is defined with a reserved keyword, while using this method name does not raise any error. Therefore, this kind of use of reserved keywords might very well be allowed in the future, because the context in which they're used makes them distinguishable from their original meaning, from the parser's point of view.Yacketyyak
[email protected] even provided a patch as a proof of concept (cf. the link in my answer), and gives an argument about why the current behaviour can sometimes be problematic: "as new reserved words are introduced, they tend to clash with existing class's method names". I'm not saying this is the way to go, but I do think that it's good to keep an open mind.Yacketyyak
Yes, they might be allowed in the future. Equally, they may never be allowed in the future. This may become a parse error in the future. "Keeping an open mind" involves preparing for both scenarios, which means following the dictates of the language to the best of your ability.Vinegarroon
S
2

You can't do that, an alternative option is to just use _case() or _new() as the function names

Also check out:

Is it possible to "escape" a method name in PHP, to be able to have a method name that clashes with a reserved keyword?

Stoa answered 14/3, 2011 at 12:28 Comment(0)
A
1

By default it is not possible, they are reserved words and you can't use them.

http://www.php.net/manual/en/reserved.keywords.php

May be you can recompile PHP or something like this to achieve your aim, but I think (as the other people that answered you) that it is not a good idea :)

HTH!

Ari answered 14/3, 2011 at 12:26 Comment(0)
R
0

Along the lines of what Benjamin mentioned, there is an interesting use of "clone", (which is reserved) in this class, line 545

 public function __call($method, $args)
{
    if ('clone' == $method) {
        return call_user_func_array(array($this, 'cloneRepository'), $args);
    } else {
        $class = get_called_class();
        $message = "Call to undefined method $class::$method()";
        throw new \BadMethodCallException($message);
    }
}
Ramp answered 28/12, 2015 at 8:36 Comment(0)
E
0

I just renamed the class to over come my problem.

foreach($array as $key => $line){
    if($key == "resource"){
        $key = "resources";
        $array->$key = $line;
    }
}
Erigeron answered 10/5, 2019 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.