开发者

Warning: Missing 1 argument

Any idea why I g开发者_运维技巧et 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");


When instantiated you did: $obj = new person(); instead of $obj = new person("joe");


$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.


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");


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


Try this code

 function __construct($persons_name= NULL) {       
            $this->name = $persons_name;        
            }

initialized NULL inside construct method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜