+ -
当前位置:首页 → 问答吧 → 关于函数__construct的问题

关于函数__construct的问题

时间:2011-09-15

来源:互联网

<?php
//人类
class person{
protected $name;
protected $age;
protected $sex;
function __construct($name,$age,$sex){
  $this->name = $name;
  $this->age = $age;
  $this->sex = $sex;
   
  }
function say(){
echo "无聊的人";
}
}
$p=new person();
$p->say();
?>
但最后显示有个错误
Warning: Missing argument 1 for person::__construct(), called in G:\www\1.php on line 17 and defined in G:\www\1.php on line 7

Warning: Missing argument 2 for person::__construct(), called in G:\www\1.php on line 17 and defined in G:\www\1.php on line 7

Warning: Missing argument 3 for person::__construct(), called in G:\www\1.php on line 17 and defined in G:\www\1.php on line 7
求解

作者: bianyu1983   发布时间: 2011-09-15

自己发贴,自己解决。

作者: bianyu1983   发布时间: 2011-09-15

$p=new person('1','2','3');

function __construct($name,$age,$sex)

作者: PhpNewnew   发布时间: 2011-09-15

你初始化类时,没有指定必要的参数。
$p=new person($name,$age,$sex);
$p->say();

作者: NET920   发布时间: 2011-09-15

function __construct($name='',$age='',$sex=''){
  $this->name = $name;
  $this->age = $age;
  $this->sex = $sex;
   
  }

作者: NET920   发布时间: 2011-09-15

热门下载

更多