13

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

Can I do that in PHP?

TRiG
  • 10,148
  • 7
  • 57
  • 107
Ciprian Mocanu
  • 2,166
  • 3
  • 25
  • 44

6 Answers6

31

No, you can't. Thank god.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    It could be quite useful, for example look at rails controllers – Alexcp Sep 07 '12 at 18:26
  • 4
    @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. – Lightness Races in Orbit Sep 14 '12 at 12:25
  • @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_"; – Josef Engelfrost Mar 08 '13 at 11:17
  • 1
    @Josef: I'm devastatingly sorry to hear that. – Lightness Races in Orbit Nov 28 '13 at 11:51
  • 1
    @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. – Josef Engelfrost Nov 28 '13 at 12:32
  • 1
    @JosefOttosson: Because of everything I already said about using reserved keywords as function names. It's just evil. Your workaround is needless and messy! – Lightness Races in Orbit Nov 28 '13 at 12:34
  • 1
    @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. – Josef Engelfrost Nov 29 '13 at 08:01
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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 2
    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. – BenMorel Jul 27 '11 at 08:55
  • cellog@php.net 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. – BenMorel Jul 27 '11 at 08:59
  • 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. – Lightness Races in Orbit Jul 27 '11 at 14:16
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?

Community
  • 1
  • 1
Stephen
  • 121
  • 1
  • 8
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!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
SubniC
  • 9,807
  • 4
  • 26
  • 33
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);
    }
}
0

I just renamed the class to over come my problem.

foreach($array as $key => $line){
    if($key == "resource"){
        $key = "resources";
        $array->$key = $line;
    }
}
bluenapalm
  • 47
  • 2