我怎么能在vscode中删除php类中的'}'预期错误,我使用了这些代码

cbwuti44  于 2023-03-21  发布在  PHP
关注(0)|答案(2)|浏览(92)
<?php
class Person{
    $age;
    $firstname;
    $lastname;
    function print_name(){
    echo $this->firstname.' '.$this->lastname.' is '.$this->age.' years old.';
    }
}

$person1 = new Person();
$person1->age = 17;
$person1->firstname = "Muller";
$person1->lastname = "Thimo";
$person1->print_name();
?>

这些是我使用的代码and these are the errors i'm getting

ejk8hzay

ejk8hzay1#

类属性应该具有public、private、protected等可见性
所以应该是

class Person{
    public  $age;
    public $firstname;
    public $lastname;
    function print_name(){
    echo $this->firstname.' '.$this->lastname.' is '.$this->age.' years old.';
    }
}

$person1 = new Person();
$person1->age = 17;
$person1->firstname = "Muller";
$person1->lastname = "Thimo";
$person1->print_name();
?>

你可以在这里阅读https://www.php.net/manual/en/language.oop5.visibility.php

7hiiyaii

7hiiyaii2#

我在PHP中预期了'}'。问题是我同时安装了两个扩展。

相关问题