PHP构造函数的一个问题
比如这一段代码classPerson{var$name;var$sex;var$age;function__construct($name,$sex,$age){$thi...
比如这一段代码
class Person{
var $name;
var $sex;
var $age;
function __construct($name,$sex,$age){
$this->name = $name;
$this->sex = $sex;
$this->age = $age;
function say(){
echo "我的名字:".$this->name.",性别:".$this->sex.",年龄:".$this->age."<br />";
}
这是一个很普通的一个类,三个变量,,一个构造函数,一个打印的函数
PHP只能有一个构造方法。我度着创了四个对象
$person1 = new Person("张三","男",20);
$person2 = new Person("张三","男");
$person3 = new Person("张三");
$person4 = new Person();
无参, 1参,2参。3参
最后都都调用say()我的结果是。都能顺便的打印出来。但是前面有一大串的警告错误代码。,感觉就是创建0 1 2 个参数的时候出现的。请问这是正常的呢。还是怎么顺事。怎么避免。
Warning: Missing argument 3 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 26 and defined in C:\xampp\htdocs\test\test.php on line 9
Notice: Undefined variable: age in C:\xampp\htdocs\test\test.php on line 12
Warning: Missing argument 2 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 27 and defined in C:\xampp\htdocs\test\test.php on line 9
Warning: Missing argument 3 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 27 and defined in C:\xampp\htdocs\test\test.php on line 9
Notice: Undefined variable: sex in C:\xampp\htdocs\test\test.php on line 11
Notice: Undefined variable: age in C:\xampp\htdocs\test\test.php on line 12
Warning: Missing argument 1 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 28 and defined in C:\xampp\htdocs\test\test.php on line 9
Warning: Missing argument 2 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 28 and defined in C:\xampp\htdocs\test\test.php on line 9
Warning: Missing argument 3 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 28 and defined in C:\xampp\htdocs\test\test.php on line 9
Notice: Undefined variable: name in C:\xampp\htdocs\test\test.php on line 10
Notice: Undefined variable: sex in C:\xampp\htdocs\test\test.php on line 11
Notice: Undefined variable: age in C:\xampp\htdocs\test\test.php on line 12 展开
class Person{
var $name;
var $sex;
var $age;
function __construct($name,$sex,$age){
$this->name = $name;
$this->sex = $sex;
$this->age = $age;
function say(){
echo "我的名字:".$this->name.",性别:".$this->sex.",年龄:".$this->age."<br />";
}
这是一个很普通的一个类,三个变量,,一个构造函数,一个打印的函数
PHP只能有一个构造方法。我度着创了四个对象
$person1 = new Person("张三","男",20);
$person2 = new Person("张三","男");
$person3 = new Person("张三");
$person4 = new Person();
无参, 1参,2参。3参
最后都都调用say()我的结果是。都能顺便的打印出来。但是前面有一大串的警告错误代码。,感觉就是创建0 1 2 个参数的时候出现的。请问这是正常的呢。还是怎么顺事。怎么避免。
Warning: Missing argument 3 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 26 and defined in C:\xampp\htdocs\test\test.php on line 9
Notice: Undefined variable: age in C:\xampp\htdocs\test\test.php on line 12
Warning: Missing argument 2 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 27 and defined in C:\xampp\htdocs\test\test.php on line 9
Warning: Missing argument 3 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 27 and defined in C:\xampp\htdocs\test\test.php on line 9
Notice: Undefined variable: sex in C:\xampp\htdocs\test\test.php on line 11
Notice: Undefined variable: age in C:\xampp\htdocs\test\test.php on line 12
Warning: Missing argument 1 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 28 and defined in C:\xampp\htdocs\test\test.php on line 9
Warning: Missing argument 2 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 28 and defined in C:\xampp\htdocs\test\test.php on line 9
Warning: Missing argument 3 for Person::__construct(), called in C:\xampp\htdocs\test\test.php on line 28 and defined in C:\xampp\htdocs\test\test.php on line 9
Notice: Undefined variable: name in C:\xampp\htdocs\test\test.php on line 10
Notice: Undefined variable: sex in C:\xampp\htdocs\test\test.php on line 11
Notice: Undefined variable: age in C:\xampp\htdocs\test\test.php on line 12 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询