简体   繁体   English

为什么我在 PHP 中收到意外的 T_NEW 错误?

[英]Why am I getting an unexpected T_NEW error in PHP?

I created a user model in Code Igniter.我在 Code Igniter 中创建了一个用户 model。

class User extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    public function new($username, $email, $password, $studentID="") {
        $this->db->query("INSERT INTO user VALUES (0, '$username', '$email', '$password', '$studentID')");
    }
}

However, I am getting this PHP error.但是,我收到此 PHP 错误。

Parse error: syntax error, unexpected T_NEW, expecting T_STRING in /home/davidfaux/testApp/application/models/user.php on line 12

Line 12, by the way, is this line.顺便说一下,第 12 行就是这一行。

public function new($username, $email, $password, $studentID="") {

What is a T_NEW?什么是 T_NEW? Why am I getting the error?为什么我会收到错误消息?

new is a reserved word for creating new objects, like new Person('Joe') . new是用于创建新对象(例如new Person('Joe')的保留字。 You should rename your function. 您应该重命名您的函数。

See http://php.net/manual/en/reserved.keywords.php for reference. 请参阅http://php.net/manual/en/reserved.keywords.php以获取参考。

These words have special meaning in PHP. 这些词在PHP中具有特殊含义。 Some of them represent things which look like functions, some look like constants, and so on--but they're not, really: they are language constructs. 它们中的一些代表看起来像函数的东西,某些看起来像常量,等等,但实际上不是:它们是语言构造。 You cannot use any of the following words as constants, class names, function or method names. 您不能将以下任何单词用作常量,类名,函数或方法名。 Using them as variable names is generally OK, but could lead to confusion. 通常将它们用作变量名是可以的,但是可能导致混乱。

new函数是用于实例化对象的保留关键字,它不是可覆盖的函数。

The accepted answer is correct.接受的答案是正确的。

But my case was different and I was redirected here, for the same error search.但我的情况不同,我被重定向到这里,进行相同的错误搜索。

My case:我的情况:
The project was working on server1.该项目正在 server1 上工作。
For R&D i uploaded this same code on server2.对于研发,我在服务器 2 上上传了相同的代码。
On server2 i faced the above error.在 server2 上,我遇到了上述错误。

Reason:原因:
"Support for 'NEW' was added in PHP 7.0" “在 PHP 7.0 中添加了对‘NEW’的支持”

So i checked my PHP version and it was lower.所以我检查了我的 PHP 版本,它更低。
Check you PHP version with phpinfo();用 phpinfo() 检查你的 PHP 版本;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM