Error Missing argument 1 for user::__construct()?

2019-09-14 01:53发布

问题:

I am recieving folloing error for my code. Please help me out guys.

Warning: Missing argument 1 for user::__construct(), called in C:\wamp\www\oop\index.php on line 8 and defined in C:\wamp\www\oop\loader\user.php on line 5

and this one

Notice: Undefined variable: Cr in C:\wamp\www\oop\loader\user.php on line 7

Index.php

<?php
function __autoload($class)
{
    include "Loader/$class.php";
} 
$user1 = new user;
echo $user1->userCreate("Blue");


?>

user.php

<?php

    class user {
        protected $userOne;
        public function __construct($Cr)
        {
            $this->userOne = $Cr;
        }
        public function userCreate() {
            return $this->userOne." User Created ";

        }
        public function userDelete() {
            return $this->userOne."user deleted";
        }

    }

?>

回答1:

You are passing parameters to wrong methods.
userCreate doesn't need a parameter but __construct does need one.

See below

$user1 = new user("Blue");
echo $user1->userCreate();


标签: php oop