search

Home  >  Q&A  >  body text

php类没输出内容

class Person{
    public $name;
    public $age;
    function _construct($name,$age){
        $this->name=$name;
        $this->age=$age;
    }
    function say(){
        echo "my name is".$this->name."my age is".$this->age."<br>";
    }
}

$person1=new Person("jack","30");
$person1->say();
$person2=new Person("mike","25");
$person2->say();

PHP新手,学习到了面向对象来了,反反复复的检查了以上代码,没发觉有什么问题,可就是不能输出名字与年龄,访问范围好像也没超出啊。输出只显示 my name is my age is 。。求解答,感激不尽,如果有时间,恳请详细解答。

PHP中文网PHP中文网2819 days ago313

reply all(3)I'll reply

  • 迷茫

    迷茫2017-04-10 15:11:00

    _construct --> __construct
    

    reply
    0
  • ringa_lee

    ringa_lee2017-04-10 15:11:00

    构造方法的名字写错了,前面是2个dash

    reply
    0
  • PHP中文网

    PHP中文网2017-04-10 15:11:00

    看来楼主还不懂什么是构造方法,看手册吧

    reply
    0
  • Cancelreply