Zacząłem bawić się w PHP OOP i PDO i piszę taki mały skrypt:
index.php
<?php include('config/db.php'); include('model/model.php'); include('controller/controller.php'); include('view/view.php'); $action = new action('localhost', 'oop', 'root', 'Nanysek93'); $action->callConnection(); new Controller(); ?>
config/db.php
<?php class connect { private $host=null; private $dbname=null; private $user=null; private $password=null; public function __construct($host, $dbname, $user, $password) { $this->host=$host; $this->dbname=$dbname; $this->user=$user; $this->password=$password; } protected function connection() { try { $this->pdo=new PDO('mysql:host='.$this->host.';dbname='.$this->dbname.'', ''.$this->user.'', ''.$this->password.''); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $ex) { } } } class action extends connect { public function callConnection() { return $this->connection(); } } ?>
controller/controller.php
<?php class controller { private $model; private $view; private $title; private $header; private $contents; public function __construct() { $this->runmodel(); $this->setdata(); $this->getdata(); $this->runview(); } private function runmodel() { $this->model=new model(); } private function setdata() { $this->model->title='Tytul'; $this->model->header='Naglowek'; $this->model->contents='Tresc'; } private function getdata() { $this->title=$this->model->gettitle(); $this->header=$this->model->getheader(); $this->contents=$this->model->getcontents(); } private function runview() { $this->view=new view($this->title, $this->header, $this->contents); } } ?>
model/model.php
<?php class model { public $title; public $header; public $contents; public function gettitle() { return $this->title; } public function getheader() { return $this->header; } public function getcontents() { return $this->contents; } } ?>
view/view.php
<?php class view { private $title; private $header; private $contents; public function __construct($title, $header, $contents) { $this->title=$title; $this->header=$header; $this->contents=$contents; $this->show(); } private function show() { } } ?>
teraz mam kilka pytań:
1. Czy dobrze rozplanowałem rozmieszczenie plików?
2. Jak mam to dalej pisać tj.: chcąc teraz dopisać jakieś pobieranie danych z bazy danych przy pomocy biblioteki PDO to gdzie co mam obsługiwać? Mam tworzyć nowe klasy czy jak to ma wyglądać?
Pozdrawiam,
adrianozo