名为 'new' 的 Codeigniter 函数
Codeigniter function named 'new'
为什么不能在控制器(可能在其他地方)中命名 Codeigniter 函数 "new"?
public function new()
{
$this->load->view('posts/new');
}
错误结果:
Parse error: syntax error, unexpected 'new' (T_NEW), expecting identifier (T_STRING)
您不仅可以在 Codeigniter
中这样做,而且在您编写的所有 PHP 中都应该 throw
出错。 new
是 PHP
中的保留字。您可以在 PHP
here.
中找到有关保留字的更多信息
人们通常试图通过在函数名称前放置一个下划线 (_
) 来克服这个问题。
function _new()
{
echo "Hello!";
}
new
是 PHP 中的保留关键字。
在 PHP docs 阅读更多相关信息。
对于那些说你做不到的人,我说我可以。有人要吗?
$test = new Test();
$test->new();
class test{
public function __call($method, $args){
if( $method == 'new' ){
echo 'Hello World';
}else{
trigger_error('Call to undefined method '.__CLASS__.'::'.$method,E_USER_ERROR);
}
}
}
魔法!
虽然我应该注意到 Codeigniter 的路由器 class 可能找不到以这种方式定义的方法。所以这不是真正的解决方案。但正如其他人提到的那样,它是 PHP 语言本身的关键字(或构造)。
就我个人而言,我可以理解限制 new 对象的使用,但是 empty()
总是让我有点生气。
Parse error: syntax error, unexpected 'empty' (T_EMPTY), expecting identifier (T_STRING)
他们真正应该解决的问题,以及 include、require 等。完全不同的上下文。
更新 CI 范围
来自https://ellislab.com/codeigniter/user-guide/general/controllers.html
私有函数
In some cases you may want certain functions hidden from public
access. To make a function private, simply add an underscore as the
name prefix and it will not be served via a URL request. For example,
if you were to have a function.
private function _utility()
{
// some code
}
Trying to access it via the URL, like this, will not work:
example.com/index.php/blog/_utility/
我只提到这个是因为 OP 声明使用 CI
为什么不能在控制器(可能在其他地方)中命名 Codeigniter 函数 "new"?
public function new()
{
$this->load->view('posts/new');
}
错误结果:
Parse error: syntax error, unexpected 'new' (T_NEW), expecting identifier (T_STRING)
您不仅可以在 Codeigniter
中这样做,而且在您编写的所有 PHP 中都应该 throw
出错。 new
是 PHP
中的保留字。您可以在 PHP
here.
人们通常试图通过在函数名称前放置一个下划线 (_
) 来克服这个问题。
function _new()
{
echo "Hello!";
}
new
是 PHP 中的保留关键字。
在 PHP docs 阅读更多相关信息。
对于那些说你做不到的人,我说我可以。有人要吗?
$test = new Test();
$test->new();
class test{
public function __call($method, $args){
if( $method == 'new' ){
echo 'Hello World';
}else{
trigger_error('Call to undefined method '.__CLASS__.'::'.$method,E_USER_ERROR);
}
}
}
魔法!
虽然我应该注意到 Codeigniter 的路由器 class 可能找不到以这种方式定义的方法。所以这不是真正的解决方案。但正如其他人提到的那样,它是 PHP 语言本身的关键字(或构造)。
就我个人而言,我可以理解限制 new 对象的使用,但是 empty()
总是让我有点生气。
Parse error: syntax error, unexpected 'empty' (T_EMPTY), expecting identifier (T_STRING)
他们真正应该解决的问题,以及 include、require 等。完全不同的上下文。
更新 CI 范围
来自https://ellislab.com/codeigniter/user-guide/general/controllers.html
私有函数
In some cases you may want certain functions hidden from public access. To make a function private, simply add an underscore as the name prefix and it will not be served via a URL request. For example, if you were to have a function.
private function _utility()
{
// some code
}
Trying to access it via the URL, like this, will not work:
example.com/index.php/blog/_utility/
我只提到这个是因为 OP 声明使用 CI