Jump to content

Can somebody please help me identify what I'm doing?


subclass

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.