3

代码在这里: $obj 是类用户的实例。我正在调用方法它没有显示输出

<?php
class user{
public  $name;
public  $age;
public function _ _construct($name, $age){
    $this->name=$name;
$this->age=$age;
}
public function sayHello(){
    echo("hiiiii".$this->name."!!!");
}
public function sayAge(){
    $a=time()-strtotime($this->age);
    echo  " hello Your age is".floor($a/(365*30*60*60));
}
}

$obj = new user('xyz','16 july 1980');
$obj->sayHello();
$obj->sayAge();

?>
4

3 回答 3

1

你的构造方法是错误的

public function _ _construct($name, $age){
    $this->name=$name;
$this->age=$age;
}

删除空间,它应该可以工作

也从$obj

于 2013-07-25T10:04:45.517 回答
0

只需$var$obj.Call it as just中删除$obj

<?php
class user{
public  $name;
public  $age;
public function __construct($name, $age){
    $this->name=$name;
$this->age=$age;
}
public function sayHello(){
    echo("hiiiii".$this->name."!!!");
}
public function sayAge(){
    $a=time()-strtotime($this->age);
    echo  " hello Your age is".floor($a/(365*30*60*60));
}
}

$obj = new user('xyz','16 july 1980');
$obj->sayHello();
$obj->sayAge();

?>
于 2013-07-25T10:03:57.123 回答
0

这是因为您在这一行有语法错误,并且您可能没有在 PHP 配置中显示调试/错误消息,所以看起来什么都没有显示:

var $obj = new user('xyz','16 july 1980');

该变量前面的“var”不是有效的 PHP 语法。

于 2013-07-25T10:04:07.263 回答