The application already has layers for database configuration and services. We are going to create layer for data structure. All files must be included and there paths are quite different. Is good idea to use namespace in these case.
project_folder
|_____config
| |_____Database.php
|
|_____service
| |_____PostService.php
|
|_____data
| |_____Post.php
|
|__________index.php
class Post {
private $category, $title, $author, $content;
private $img, $id;
public function __construct($category='', $title='', $author='', $content='') {
$this->category = $category;
$this->title = $title;
$this->author = $author;
$this->content = $content;
}
function getCategory() {
return $this->category;
}
function getTitle() {
return $this->title;
}
function getAuthor() {
return $this->author;
}
function getContent() {
return $this->content;
}
function getImg() {
return $this->img;
}
function setCategory($category) {
$this->category = $category;
}
function setTitle($title) {
$this->title = $title;
}
function setAuthor($author) {
$this->author = $author;
}
function setContent($content) {
$this->content = $content;
}
function setImg($img) {
$this->img = $img;
}
function getId() {
return $this->id;
}
function setId($id) {
$this->id = $id;
}
}
include('./data/Post.php');
class PostService {
private $conn;
private $database;
public function __construct(Database $database) {
$this->database=$database;
$this->conn = $database->getConnection();
}
public function readPost($id) {
$query = "SELECT * FROM post WHERE postID=?";
$form_data = [$id];
$statement = $this->conn->prepare($query);
$data=new Post();
if ($statement->execute($form_data)) {
foreach ($statement->fetchAll() as $row) {
$data->setId($row['postID']);
$data->setAuthor($row['author']);
$data->setCategory($row['category']);
$data->setTitle($row['title']);
$data->setContent($row['content']);
$data->setImg($row['img']);
}
}
return $data;
}
}
This is the test in browser.
Post Object ( [category:Post:private] => math [title:Post:private] => First Post [author:Post:private] => Author [content:Post:private] => lorem ipsum... [img:Post:private] => [id:Post:private] => 1 )
<?php
namespace config;
/**
* add namespace with the folder name
* add row for PDO
*/
use \PDO;
class Database {
private $connection = '';
private $host = "localhost";
// Other part of code is not changed
<?php
namespace data;
/**
* add namespace with folder name
*/
class Post {
//....continue without changes
<?php
namespace service;
include('./data/Post.php');
use data;
use config;
class PostService {
private $conn;
private $database;
public function __construct(\config\Database $database) {
$this->database = $database;
$this->conn = $database->getConnection();
}
public function readPost($id) {
$query = "SELECT * FROM post WHERE postID=?";
$form_data = [$id];
$statement = $this->conn->prepare($query);
$data = new \data\Post();
if ($statement->execute($form_data)) {
foreach ($statement->fetchAll() as $row) {
$data->setId($row['postID']);
$data->setAuthor($row['author']);
$data->setCategory($row['category']);
$data->setTitle($row['title']);
$data->setContent($row['content']);
$data->setImg($row['img']);
}
}
return $data;
}
}
?>
<?php
//this is index.php
include 'config/database.php';
$db = new \config\Database();
include 'service/PostService.php';
$s = new \service\PostService($db);
$postID=1;
print_r($s->readPost($postID));
?>
We have the same result!
Post Object ( [category:Post:private] => math [title:Post:private] => First Post [author:Post:private] => Author [content:Post:private] => lorem ipsum... [img:Post:private] => [id:Post:private] => 1 )