The goal of layered software architectural style is to divide the tasks for visualisation, data retrieving and handling. How to make simple PHP application with layered architecture? Here is one solution.
DROP DATABASE IF EXISTS `blog`;
CREATE DATABASE IF NOT EXISTS `blog` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `blog`;
DROP TABLE IF EXISTS `post`;
CREATE TABLE IF NOT EXISTS `post` (
`postID` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`author` varchar(255) NOT NULL,
`content` text NOT NULL,
`img` varchar(255) NOT NULL,
`category` varchar(255) DEFAULT NULL,
PRIMARY KEY (`postID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `post` (`postID`, `title`, `author`, `content`, `img`, `category`) VALUES
(1, 'First Post', 'Author', 'lorem ipsum...', '', 'math'),
(2, 'Second Post', 'Author', 'text, text, text', '', 'math');
project_folder
|_____config
| |_____Database.php
|
|_____service
| |_____PostService.php
|
|___________index.php
<?php
class Database {
private $connection = '';
private $host = "localhost";
private $username = "root";
private $password = "";
private $database = "blog";
//private $charset = 'utf8mb4';
public $opt = [
PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
function __construct() {
$this->database_connection();
}
function database_connection() {
$this->connection = new \PDO("mysql:host=" . $this->host . ";dbname="
. $this->database, $this->username, $this->password, $this->opt);
$this->connection->exec("set names utf8");
}
function getConnection() {
return $this->connection;
}
}
?>
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 = array();
if ($statement->execute($form_data)) {
foreach ($statement->fetchAll() as $row) {
$postID = $row['postID'];
$data[$postID]['title'] = $row['title'];
$data[$postID]['content'] = $row['content'];
}
}
return $data;
}
}
<?php
include 'config/Database.php';
include 'service/PostService.php';
$db= new Database();
$s= new PostService($db);
$id=1;
print_r($s->readPost($id));
?>
This is the result:
Array ( [1] => Array ( [title] => First Post [content] => lorem ipsum... ) )
class Post{};
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 = array();
if ($statement->execute($form_data)) {
// foreach ($statement->fetchAll() as $row) {
// $postID = $row['postID'];
// $data[$postID]['title'] = $row['title'];
// $data[$postID]['content'] = $row['content'];
// }
$data=$statement->fetchObject(Post::class);
}
return $data;
}
}
Post Object ( [postID] => 1 [title] => First Post [author] => Author [content] => lorem ipsum... [img] => [category] => math )