Method name clashing with language construct

386 views Asked by At

I simply want to name a method in my class list(). If I try and do so:

class MyClass {

  function list () {
    // Do some stuff here
  }

}

...I get a parse error (Unexpected T_STRING...). The same is true if I use echo or print - so I am guessing that I am not allowed to name a class method the same as a language construct, and while I cannot find any documentation that explicitly states this to be the case, I suppose it does make sense.

My question is, does anyone know of a work around to this, that will work in PHP 4.3.10? I know this can be done in PHP 5.3+ with closures (or I assume it can, I haven't actually tried) No versions of PHP to date support doing this with closures, but can anyone think of a work-around that does not rely on this, baring in mind that it is the method name that is important here?

EDIT N.B.

I am fully aware of how ancient and dead PHP4 is, but I have no option for this particular project. It is to be run on a platform distributed with PHP 4.3.10, which is a very low resource BusyBox platform with a MIPS processor, for which no compilers are provided. I have managed to create a cross-compiler and successfully build PHP 5.2 for it (I haven't tried 5.3 yet, it would probably involve compiling a newer Apache as well) but regardless of this, the manufacturer insist that this invalidates their warranty.

4

There are 4 answers

1
hakre On BEST ANSWER

The dilemma you face will stay until you choose A or B.

  • A: Choose another function name, one that is not a reserved word.

  • B: Change PHP so that it provides the language features you need.

__call is not available in PHP 4. list is a reserved word since long time.

And you have not outlined what your specific problem with the class interface is, so I don't see much room for more alternatives.

As B does not look like a real option here, you need to take A.

Hope this helps that you can come to a conclusion.

5
GordonM On

You've got two options.

1) rename your method. Unless there's a real reason you can justify your method's name then this is the one you should do.

2) namespace your class. If you wrap your class in its own namespace then you can reuse names that are normally reserved or predefined functions, or defined elsewhere. However you should only use this approach if you know what you're doing, otherwise you can end up causing a lot of confusion.

I'd opt for option 1 unless you have a really really compelling reason why your method name must have the same name as a PHP reserved word.

EDIT: You're using PHP 4 so option 2 is off the table. Your only choice is using a different name for your method. That, or upgrade to PHP 5.3. Even if you don't go with namespacing in the end I'd strongly advise upgrading to PHP 5.3 anyway, as PHP 4 is dead and has been for a long time. There will be no security patches released for it ever again so by running it you're basically making your server very insecure.

0
fidr On

Kind of a dirty hack, but you could use the magic methods. PHP4 doesn't really support the __call like php5 does, but it could work with the 'overload' construct. I haven't tested this so I can't be sure..

( http://php.net/manual/en/function.overload.php )

class MyClass {
  function __call($func, $args){
    if ($func == 'list')
      return $this->_list($args);      
  }

  function _list () {
    // Do some stuff here
  }
}

overload('MyClass');

$myclass = new MyClass();
$myclass->list();
0
Gordon On

Reserved keywords are reserved:

you cannot use any of the following words as constants, class names, function or method names

So rename your methods.