I'm working on a project today and i've been working for a while now and i don't see what i do wrong here. Can someone give me the right example.
Thnx a lot!
Connector:
class Repository
{
private $connector;
public function __construct(Config $connector)
{
$this->connector = $connector;
}
public function events()
{
$query = 'SELECT * FROM digi_gz_parties';
$dbh_query = $this->connector->getDatabase()->prepare($query);
$dbh_query->execute();
$dbh_querys = $dbh_query->fetchAll();
return $dbh_querys;
}
}
Getter:
class REST
{
public function getEvents()
{
require 'logic/Repository.php';
$event = new Repository();
$events = $event->events();
return $events;
}
}
Error: Argument 1 passed to Repository::__construct() must be an instance of Config.
I know i need to give a paramater to the repository but i don't want it, i want only to call the repository without give some paramter.
Thanks a lot!
You can change your Repository::__construct()
to have a default $connector
to null
:
public function __construct(Config $connector = null)
{
$this->connector = $connector;
}
That way, if you instanciate your object without any parameter, like you do here, it will default to null
. The only downside of that is that, now, you have to be extra careful when using $this->connector
inside your Repository
class and remember it could be null.
For exemple, here, the second line of your events()
method is not going to work, because you lack the proper configuration to connect to your database.