couldn't find an answer for my question so here it goes.
I have a config.php file with the information for database connection:
<?php
declare(strict_types=1);
namespace App\Model;
// COMPOSER AUTOLOAD
require '../vendor/autoload.php';
// ROUTER FOR CONFIG PAGE
class Config
{
// LOCAL DATABASE
public $Database_Name = "power";
public $Database_User = "root";
public $Database_Password = "";
}
I have a Database.php file with the PDO connection:
<?php
declare(strict_types=1);
namespace App\Model;
use PDO;
use Exception;
include "../public/Config.php";
// CLASSE DATABASE
class Database
{
private static $bdd = null;
// MAIN DATABASE CONNECTION USED BY ALL METHODS
public static function getBdd()
{
if (self::$bdd == null) {
$config = new Config;
try {
self::$bdd = new PDO('mysql:host=localhost; dbname=' . $config->Database_Name, $config- >Database_User, $config->Database_Password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
} catch (Exception $e) {
echo "Cannot reach Database";
die;
}
}
return self::$bdd;
}
}
I have my Class with getters and setters that i want to instanciate and fill with the data from the database:
<?php
declare(strict_types=1);
namespace App\Model;
class Group
{
private $id;
private $group_admin;
private $creation_date;
private $group_name;
private $group_description;
private $group_status;
// CONSTRUCT
public function __construct($id, $group_admin, $creation_date, $title, $description, $status)
{
$this->id = $id;
$this->group_admin = $group_admin;
$this->creation_date = $creation_date;
$this->group_name = $title;
$this->group_description = $description;
$this->group_status = $status;
}
/**
* Get the value of id
*/
public function getId()
{
return $this->id;
}
/**
* Set the value of id
*
* @return self
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
..........
And finally i have my Models with the code i want to execute to get the data and fill my classes:
<?php
namespace App\Model;
use PDO;
use App\Model\Group; // THIS SHOULD IMPORT THE GROUP CLASS
class GroupModel
{
public function getMyGroups(): array
{
$bdd = Database::getBdd();
$req = $bdd->prepare("SELECT * FROM groups ORDER BY creation_date DESC");
$req->execute();
// DEBUG
// $req->debugDumpParams();
// die;
$Group = new Group(null, null, null, null, null, null);
$result = $req->fetchall(PDO::FETCH_CLASS, 'Group');
var_dump($result);
die;
return $result;
}
... problem is, the class is unseen by my code.
What am i missing here?
I currently get the error:
Fatal error: Class 'Group' not found in D:\Dropbox\...\...\OCR-P5\src\Model\GroupModel.php on line 43
I also tryed with Group::class or by instantiating the class but no, like this:
$result = $req->fetchall(PDO::FETCH_CLASS, Group::class );
It doesn't work either and cannot find the class.
I also noticed that i am using the PDO expression in two places (1 - in the file that establishes the connection, 2 - in the last Model method fetchall(), not sure if i should.
Best regards.