实例和静态函数的重新声明

发布于 2024-11-04 20:13:44 字数 396 浏览 5 评论 0原文

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();

致命错误:无法重新声明 me::work()

问题是,为什么 php 不允许这样的重新声明。有什么解决方法吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女净妖师 2024-11-11 20:13:44

实际上,使用魔术方法创建可以解决此问题,尽管我很可能永远不会在生产代码中执行类似的操作:

触发 __call当在对象范围内调用不可访问的方法时在内部。

当在静态范围内调用不可访问的方法时,__callStatic 在内部触发。

<?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');

?>

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');

?>
嘿看小鸭子会跑 2024-11-11 20:13:44

我认为你应该这样做:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文