Home > Backend Development > PHP Tutorial > php类没输出内容

php类没输出内容

WBOY
Release: 2016-06-06 20:40:53
Original
1045 people have browsed it

<code>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();
</code>
Copy after login
Copy after login

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

回复内容:

<code>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();
</code>
Copy after login
Copy after login

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

<code>_construct --> __construct
</code>
Copy after login

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

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template