php 警告:缺少 1 个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4520605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: Missing 1 argument
提问by Phil
Any idea why I get this error:
知道为什么我会收到此错误:
Warning: Missing argument 1 for person::__construct(), called in /home/fishbein/public_html/dev/OOP/index.php on line 5 and defined in /home/fishbein/public_html/dev/OOP/class_lib.php on line 6
Warning: Missing argument 1 for person::__construct(), called in /home/fishbein/public_html/dev/OOP/index.php on line 6 and defined in /home/fishbein/public_html/dev/OOP/class_lib.php on line 6
With this code:
使用此代码:
<?
class person {
var $name;
function __construct($persons_name) {
$this->name = $persons_name;
}
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
I'm also using this in my index file:
我也在我的索引文件中使用它:
$tyler = new person("Tyler");
回答by Babiker
When instantiated you did: $obj = new person();
instead of $obj = new person("joe");
实例化时你做了:$obj = new person();
而不是$obj = new person("joe");
回答by Dejan Marjanovic
$persons_name = ""
Set it like this in argument. But that is not solution. You can remove construct, make new instance and then set name. If yours somehow doesn't work.
在参数中像这样设置它。但这不是解决方案。您可以删除构造,创建新实例,然后设置名称。如果你的不知何故不起作用。
回答by brian_d
You are calling the constructor without passing in an argument. Perhaps you are doing something like
$p = new person();
instead of $p = new person("theirName");
您正在调用构造函数而不传入参数。也许你正在做类似的事情
$p = new person();
而不是$p = new person("theirName");
回答by Giri
It seems: If the class name is the same as the function name this warning is given. If you name the function differently from class name it seems OK. You need to supply arguments only when calling he function not at the class instantiation
似乎:如果类名与函数名相同,则会给出此警告。如果您将函数命名为与类名不同的名称,则似乎没问题。仅在调用函数时才需要提供参数,而不是在类实例化时
回答by shahzad Dar
Try this code
试试这个代码
function __construct($persons_name= NULL) {
$this->name = $persons_name;
}
initialized NULL inside construct method.
在构造方法中初始化 NULL。