ПОЛИТИКА ЗА ПОВЕРИТЕЛНОСТ И ЗАЩИТА НА ЛИЧНИ ДАННИ

PHP App with Layered Archtiecture - Step 2

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.

Create data layer

  1. Create folder data
  2. Add class file post with fields corresponding with fields of table post in the database.
						
blog
|_____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;
	}


}							
						

Change Service to return object Post

  1. Add fields
  2. Include class file for data structure
  3. Test with index.php
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 )

Start using namespace

  1. Change Database class
    <?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										
    								
  2. Change Data class
    <?php
    namespace data;
    
    /**
     * add namespace with folder name
     */
    class Post {
    //....continue without changes										
    								
  3. Change Service class
    
    <?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;
    	}
    }
    ?>
    								
  4. Test with index.php - including has changes!
    
    	<?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 )