doudao9915 2014-10-03 16:51
浏览 32
已采纳

PHP保留的方法名称

Other than those described by http://php.net/manual/en/reserved.keywords.php, are there any method names I shouldn't use? For instance, can I make a method public function echo($x) and use it as $this->echo('Hello');?

EDIT> Evidently, I can't use echo because it is a language constructs, but could use min. Following doesn't result in an error.

<?php
  class myClass {
      public function min($x){echo(min($x));}
  }

  $obj=new myClass();
  $obj->min(array(5,3,6,3));
?>
  • 写回答

2条回答 默认 最新

  • dongxun2089 2014-10-03 17:42
    关注

    You can't use language construct names in a classes regarding namespaces, while you can use built-in function names in namespace. A namespace in this case is a method of a class.

    $foo = new Foo();
    
    $foo->array_key_exists(); // Good
    $foo->isset(); // Fatal error, since isset() a language construct
    $foo->include(); // Also fatal error
    

    The reason you don't get a fatal error when using min() is because its a built-in function.

    Generally it's considered bad practice using built-in names in your own implementations because that might confuse another developers that will be reading your code in future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用