A
A
alestro2015-12-18 13:16:02
PHP
alestro, 2015-12-18 13:16:02

The variable does not come to the constructor, how to fix it?

There is a category model, it has a constructor, in which the database object should come, but when this model (Categories) is instantiated, it produces:
Catchable fatal error: Argument 1 passed to models\Categories::__construct() must be an instance of core\Database , none given in
I remove the type hint and get a warning level error:
Missing argument 1 for models\Categories::__construct()

public function __construct(\core\Database $db){
    $this->db=$db;
}//Конструктор класса models\Categories
var_dump($register->getDb());//object(core\Database)#1 (0) { } 
$categories=new\models\Categories($register->getDb());

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fellowship, 2015-12-18
@Fellowship

Most likely an error elsewhere in the code, as pointed out by 27cm, or for some reason $register->getDb() can return an object only once.
Offtopic: read about SIngleton, and in general it is better to wrap work with the base in static methods. This will make it easier to work with the database in the future and not to drive a link to the database object every time in each object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question