Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我不明白我为什么会犯这个错误_Php_Compiler Errors - Fatal编程技术网

Php 我不明白我为什么会犯这个错误

Php 我不明白我为什么会犯这个错误,php,compiler-errors,Php,Compiler Errors,我知道这是一个语法错误,但我不知道我所做的有什么错。错误是 分析错误:语法错误,意外的T_常量\u封装的_字符串,第8行应为T_字符串或T_变量或“{”或“$” 代码是 class Person { public $isAlive=true; public $firstname; public $lastname; public $age; public function __construct() { $teacher->"b

我知道这是一个语法错误,但我不知道我所做的有什么错。错误是

分析错误:语法错误,意外的T_常量\u封装的_字符串,第8行应为T_字符串或T_变量或“{”或“$”

代码是

   class Person {
    public $isAlive=true;
    public $firstname;
    public $lastname;
    public $age;
    public function __construct()
    {
    $teacher->"boring"=$firstname;
    $teacher->"12345"=$lastname;
    $teacher->12345=$age;
    $student->"Natalie Euley"=$firstname;
    $student->"Euley"=$lastname;
    $student->19=$age;
    }
    public function greet()
    {
    return "Hello, my name is ".$this->firstname." ".$this->lastname. "Nice to meet you!";
    }
    }
    $student = new Person();
    $teacher = new Person();
    echo $student->greet();
    echo $techer->greet();

我现在明白了。CodeAcademy有令人困惑的方向。我现在知道怎么做了。谢谢你解释一切!

你应该这样做:

$teacher->"boring" = $firstname;
像这样:

$this->firstname = "boring";
$student->"Natalie Euley"=$firstname;
对于剩下的代码,您需要的是这样的方式:

public function __construct($firstname, $lastname, $age)
{
    $this->firstname = $firstname;
    $this->lastname  = $lastname;
    $this->age       = $age;
}

$teacher = new Person("John", "Smith", 45);

你的语法不正确

$this->firstname = "boring";
$this->lastname = "12345";
如果要将这些值分配给所属的类,我们将使用此选项

就这样

$object->variable = value;
这些都是错误的

$teacher->"boring"=$firstname;
$teacher->"12345"=$lastname;
$teacher->12345=$age;
$student->"Natalie Euley"=$firstname;
$student->"Euley"=$lastname;
$student->19=$age;
应该是

$teacher->firstname = "boring";
$teacher->lastname = "12345";
$teacher->age = 12345;
$student->firstname = "Natalie Euley";
$student->lastname ="Euley";
$student->age = 19;
检查这里

像这样的东西:

$this->firstname = "boring";
$student->"Natalie Euley"=$firstname;
是无效的,你可能是说

$student->firstname = "Natalie Euley";
不能将字符串用作那样的对象键引用。但可以使用:

$student->{"Natalie Euley"} = $firstname
          ^--             ^--note the brackets

但是,这仍然是向后的。像这样的赋值应该在key=>$value完成,而您在做$value=>key,这是bass ackwards。您的构造函数方法中有语法错误。例如,下面这行是错误的PHP代码:

$student->"Natalie Euley"=$firstname;
我建议阅读以下官方文件:

以下改进的you示例代码运行良好:

class Person {
    public $isAlive = true;
    public $firstName;
    public $lastName;
    public $age;

    public function __construct($firstName, $lastName, $age) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->age = $age;
    }

    public function greet() {
        return 'Hello, my name is ' . $this->firstName . ' ' . $this->lastName .
            ' and I\'m '. $this->age . ' years old. Nice to meet you!';
    }
}

$student = new Person('Max', 'Kid', 19);
$teacher = new Person('Albert', 'Einstein', 60);
echo $student->greet() . "\n";
echo $teacher->greet();

你的类成员变量赋值语法无效。事实上,它们是反向的。我以前从未见过。这是一次令人印象深刻的尝试wrongness@Dagon:这有什么建设性?每个人都曾经是初学者,而且人们并不生来就知道PHP及其语法。我同意这实际上是一个低质量的问题,但我个人感觉到了这样的评论轻浮,你会活得更长。