Uday Hirawale
Uday Hirawale

Reputation: 47

Creating class object inside another class method giving fetal error

I am getting this error

Catchable fatal error: Argument 1 passed to classes\session::session_check() must be an instance of classes\conf\collections, none given, called in /var/www/html/test1/index.php on line 48 and defined in /var/www/src/classes/session.php on line 18

My collections class is

namespace classes\conf{
        class collections{
            private $mongo;

            public function __construct(){
                $this -> mongo = new \MongoClient();
            }

            public function members(){
                return $this -> mongo -> publicData -> members;
            }
        }
    }

My session class is

namespace classes{
        use \classes\conf\collections;

        class session{
            private $session_id;
            private $session_token;
            private $cookie_token;

            public function __construct(){
                $this -> session_id = session_id();
                $this -> session_token = (isset($_SESSION['token'])) ? $_SESSION['token'] : NULL;
                $this -> cookie_token = (isset($_COOKIE['token'])) ? $_COOKIE['token'] : NULL;

                echo 'Hey';
            }

            public function session_check(collections $collection){
                $data = $collection -> members() -> findone(
                    array(
                        'account.username' => 'uday'
                    ),
                    array(
                        '_id' => 1
                    )
                );

                return true;
            }
        }
    }

I am using spl_autoload to include classes. Everything is working fine if I create new object outside session_check function and pass as variable. But this should work too. Can anybody explain why it is showing fetal error.

Why collections $collection is not creating object as error shows none given?

Upvotes: 0

Views: 65

Answers (1)

hynner
hynner

Reputation: 1352

The cause of your error is the code that calls session_check.

public function session_check(collections $collection){

doesn't create new collections but requires that argument of type collections is passed when calling the function.

Therefore you have to do something like this:

$session = new session();
$collections = new collections();
$session->check_session($collections);

Upvotes: 1

Related Questions