subclass Posted June 25, 2009 Share Posted June 25, 2009 Hi All - First Post, I've been going mental with my PHP development, and seriously feel like I need a better solution as I'm coding it new from scratch almost every time and it's draining my time and efforts. Was wondering if this kind of approach means anything to anyone / if they could make some recommendations on what the hell I'm doing. It's feeling terribly procedural using static classes as wiring or builders/fetchers for objects I'm basically splitting my application into 4x parts 1. Objects - basically properties and a constructor (no clever stuff) 2. Wiring - classes to construct objects (run db query, get data, create 'new' objects & return) 3. Templates - no php code here, just placeholders 4. Index - .htaccess rewrites all requests (except images/robots.txt/admin/etc) Objects -------- page.php class Page { public $id; // autogen from mysql public $name; public $url; public $seotitle; public $seodescription .... public function __construct ( $id,$name,$url,$seotitle,$seodescription,.... ) { $this->id = $id; $this->name = $name; .... } } Wiring ------- pages.php class Pages { static function get_page($id) { $query = "SELECT * FROM `pages` WHERE `id` = {$id}"; $res = mysql_query($query); $row = mysql_fetch_object($res); switch($row->type) { case "homepage": return new homepage($row->id, $row->name, $row->url ....); break; case "page": return new page($row->id, $row->name, $row->url ...); } } static function get_all_type_pages($type) { // select * from pages where `type`= $type // return array of page objects } } Templates ----------- basically html files with placeholders %field1%, %field2%, <title>%seotitle</title> etc Main File (index.php) ---------------------------- require(settings.php); // domain, mysql, constants etc require(pages.php); // get page id $request = $_SERVER['REQUEST_URI']; $query = "SELECT * FROM `pages` WHERE BINARY `url` = '{$request}'"; $res = mysql_query($query); if(mysql_num_rows($res) < 1) { //throw 404/intelligent sitemap exit(); } $page = Pages::get_page($id); switch($page->type) { case "homepage": $page['content1'] = $page->content1; $page['content2'] = $page->content2; $output = file_get_contents("templates/homepage"); break; } foreach(array_keys($page) as $key) { $output = preg_replace("/%$key%/",$page[$key],$output); } echo $output; Any feedback much appreciated ??? Link to comment https://forums.phpfreaks.com/topic/163692-can-somebody-please-help-me-identify-what-im-doing/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.