3
class me {
   private $name;
   public function __construct($name) { $this->name = $name; }
   public function work() {
       return "You are working as ". $this->name;
   }
   public static function work() {
       return "You are working anonymously"; 
   } 
}

$new = new me();
me::work();

Fatal error: Cannot redeclare me::work()

the question is, why php does not allow redeclaration like this. Is there any workaround ?

Rizky Ramadhan
  • 7,360
  • 4
  • 27
  • 31
  • 4
    Yes there's a workaround: use another name. ;-) PHP is not C++, methods are unique by their names, not by their name/arguments/visibility combination. Even then, you cannot overload an object method to a static method in C++ either. – netcoder May 03 '11 at 01:24
  • Does any language allow this? I know C# does not - http://stackoverflow.com/questions/160118/static-and-instance-methods-with-the-same-name – Phil May 03 '11 at 01:25
  • but it's neat to use same name to do same thing isn't it? – Rizky Ramadhan May 03 '11 at 01:28
  • @Rizky Not if they do different things – Phil May 03 '11 at 01:29
  • @Rizky: Not really, it just creates ambiguity for no reason, because if both methods would do the exact same thing, you wouldn't need both. Just make the constructor's `$name` argument an optional parameter and put an `if` in your `work()` method. – netcoder May 03 '11 at 01:30
  • @karim79: It's true that it's related but the code linked there is actually a bad thing. Strict standards forbids a call to a method that is declared non-static, and PHP may die with a fatal error if you try to call a static method in object context (when using `$this` in a static method). – netcoder May 03 '11 at 01:37
  • @Phil Ruby lets you do this, and there are quite a few instances where it's a completely sensible thing to do. –  Jun 19 '13 at 17:29
  • possible duplicate of [PHP - is it possible to declare a method static and nonstatic](http://stackoverflow.com/questions/11331616/php-is-it-possible-to-declare-a-method-static-and-nonstatic) – Magnar Myrtveit Jul 15 '14 at 20:18

2 Answers2

7

There is actually a workaround for this using magic method creation, although I most likely would never do something like this in production code:

__call is triggered internally when an inaccessible method is called in object scope.

__callStatic is triggered internally when an inaccessible method is called in static scope.

<?php

class Test
{
    public function __call($name, $args)
    {
        echo 'called '.$name.' in object context\n';
    }

    public static function __callStatic($name, $args)
    {
        echo 'called '.$name.' in static context\n';
    }
}

$o = new Test;
$o->doThis('object');

Test::doThis('static');

?>
Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
-2

Here is how I think you should do it instead:

class me {
   private $name;

   public function __construct($name = null) { 
       $this->name = $name; 
   }

   public function work() {
       if ($this->name === null) {
           return "You are working anonymously"; 
       }
       return "You are working as ". $this->name;
   }
}

$me = new me();
$me->work(); // anonymous

$me = new me('foo');
$me->work(); // foo
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • 1
    The "anonymous" one is not static. – Malfist Jan 09 '14 at 19:59
  • @Malfist: It never claimed to be. It's an alternative solution. Read the answer properly next time. Static methods should be avoided as much as possible, and the OP's use case is not one where a static method is warranted. – netcoder Jan 09 '14 at 21:22
  • Why should static functions be avoided as much as possible? There are perfectly good reasons for static functions, plus the reduce complexity by reducing the scope. Static functions are good programming. That being said, OP's use case was not one where static was warranted, I agree, but it may have just be a sample the OP thought up to exhibit the behavior he wants. – Malfist Jan 09 '14 at 21:32
  • @Malfist: Not going to argue with someone who says things like "static functions are good programming" (what does that even mean?). Besides, there's plenty to read about it. And it doesn't change the fact that you totally missed the point. – netcoder Jan 09 '14 at 23:07