I'm trying to learn OOP with PHP (I know it's hard because It's not an OOP language). I'm rewriting my procedural code to OOP.
Please provide any feedback on my code, naming and file/folder structure.
Structure:
\classes
Procurement.php
ProcurementDetails.php
\services
DatabaseService.php
ProcurementService.php
ProcurementsService.php
\public
Index.php
\routes
Procurements.php
classes\Procurement.php
<?php
class Procurement {
public int $id;
public null | array | string $group;
public null | string $object_name;
public null | string $procurement_type;
public null | string | object $responsible_user;
public null | string $announcement_time;
public null | string $status;
public null | string | object $creator;
public function __construct()
{
if ($this->responsible_user) $this->responsible_user=json_decode($this->responsible_user);
if ($this->group) $this->group=json_decode($this->group);
if ($this->creator) $this->creator=json_decode($this->creator);
}
private function __set($name, $value) {}
};
classes\ProcurementDetails.php
<?php
require_once __DIR__ . '/Procurement.php';
class ProcurementDetails extends Procurement {
public null | string $description;
public null | int $sum;
public null | string $procurement_method;
public null | string | array $contact_person;
public function __construct()
{
if ($this->contact_person) $this->contact_person=json_decode($this->contact_person);
parent::__construct();
}
}
services\DatabaseService.php
<?php
interface IDatabaseService {
/**
*
*
* @return PDO
*/
public function GetPDO();
}
class DatabaseService implements IDatabaseService {
/**
* PDO object
*
* @var PDO
*/
private $DB;
private $host = '';
private $dbname = '';
private $user = '';
private $pass = '';
/**
* Creates the connection
*
* @return void
*/
public function __construct () {
$this->DB = new PDO("mysql:host={$this->host};dbname={$this->dbname};charset=utf8mb4",$this->user,$this->pass);
$this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->DB->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->DB->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
}
public function getPDO() {
return $this->DB;
}
/**
* __destruct
*
* @return void
*/
public function __destruct () {
$this->DB=null;
}
}
services\ProcurementsService.php
<?php
require_once __DIR__ . '/../classes/Procurement.php';
require_once __DIR__ . '/ProcurementService.php';
interface IProcurementsService {
/**
* Returns all procurements from database
* Procurement[]
*
* @return Procurement[]
*/
public function GetAllProcurements();
/**
* GetById
*
* @param int $id
* @return IProcurementService
*/
public function GetById(int $id);
}
class ProcurementsService implements IProcurementsService {
private PDO $_db;
/**
* __construct
*
* @param PDO $db
* @return void
*/
public function __construct(PDO $db)
{
$this->_db = $db;
}
/**
* GetById
*
* @param int $x
* @return IProcurementService
*/
public function GetById(int $id): IProcurementService {
return new ProcurementService($id, $this->_db);
}
/**
* Returns all procurements from database
* Procurement[]
*
* @return Procurement[]
*/
public function GetAllProcurements(): iterable {
$stmt=$this->_db->query("SELECT ....");
return $stmt->fetchAll(PDO::FETCH_CLASS, 'Procurement');
}
}
classes\ProcurementService.php
<?php
require_once __DIR__ . '/../classes/ProcurementDetails.php';
interface IProcurementService {
/**
* Returns more info about the procurement
*
* @param int $id
* @return ProcurementDetails
*/
public function GetDetails();
public function GetFiles(); //Not implemented
public function GetComments(); //Not implemented
}
class ProcurementService implements IProcurementService {
private int $id;
private PDO $_db;
public function __construct(int $id, PDO $db)
{
$this->id = $id;
$this->_db = $db;
}
/**
* Returns more info about the procurement
*
* @param int $id
* @return ProcurementDetails
*/
public function GetDetails(): ProcurementDetails
{
$stmt = $this->_db->prepare("SELECT with details....");
$stmt->execute([$this->id]);
return $stmt->fetchObject('ProcurementDetails');
}
}
routes\Procurements.php
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
require_once __DIR__ . '/../services/DatabaseService.php';
require_once __DIR__ . '/../services/ProcurementsService.php';
$app->get('Procurements', function(Request $request, Response $response) {
$DB=new DatabaseService();
$ProcurementsService = new ProcurementsService($DB->getPDO());
$response->getBody()->write(json_encode($ProcurementsService->GetById(100)->GetDetails()));
return $response->withHeader('content-type','application/json')->withStatus(200);
});
I felt like I got off track with OOP, so I don't have much code. BTW! I am using PHPSLIM.