Using OOP to build a CMS?

Hello All,

Want to pick your brains. I’ve written 5-6 CMS systems using procedural php, and can build them quite easily. Now at this moment in time I also want to learn OOP, so I figured I learnt procedural php by creating a CMS, so why not do the same for OOP but via a very simple CMS to start off with!

Now, all the examples i’ve found for OOP pee me off a little bit. Like for example how to make a dog bark (yes that’s going to really help me in the real world!).

So what I am after is maybe some advice as to how to go about it while building a simple CMS containing one form only for the time being. Now this is what i’m thinking to start off with.

  1. I create a class to connect to the database
  2. I create a class to output a single form to a page
  3. I create a class that allows me to insert or update data in the database

All like so:


class DBConnect
{ 
// create a database object 
function database($host, $dbname) { } 
// connect to the database 
function connect($username, $password) { } 
// are we connected? 
function isConnected() 
// disconnect from the database 
function disconnect() { } 
// execute a query and return the result 
function query($sql) { } 
} 



class Forms 
{ 
//Outputs a form to the page
function createForm() {}
} 


class Statements
{ 
//Inserts data into the database table
function Insert() {}
//Updates the database table
function Update() {}
} 

Is this a good starting block, or am I totally heading in the wrong direction? Any thoughts? Is using OOP to build a CMS overkill?

Thanks

Good luck to your OOP CMS. I always favor to build new things - just because the already existing apps are not enough - they may be too generic and less useful in a specialized business process. This could be one reason why so many apps with similar working came up.

Even if you want to write your own things, at least revise codes from the the already popular CMS tools. You will certainly get a chance to improve or use most of their soruce codes as well.

For example, you can learn how to re-use a table for category/parent category of everything, if you look into Wordpress. And, get several useful APIs from Code Igniter.

Further, Look into object oriented MVC patterns and template engines - which will progress your work.

Your best bet is to look at some of the oop cms systems and see how they do it. Save yourself the time of re-inventing everything yourself.

Hear Hear!!

I see all these things trying to be everything to all people and they always end up being overblown huge applications that aren’t particularly great at anything in specific.

As far as CMS… I use the filesystem (with revision control) and I love it, it works, it’s simple and I can still use PHP for things like standardized headers/footers and components.

In a lot of ways, I consider PHP “out of the box” to be a pretty good CMS.

To the OP, why not start with an ultra simple “CMS”:


// sort of like one of my "cms" it's simple, but it gets the job done.
$page = page();
$page->title('My Page Title');
$page->header();

.. content ...

$page->footer();

Play around with it for awhile. I’ve used variations of the above in many situations, it’s also a good way to create very fast mock-ups for perspective clients.

I know it looks “too simple”, but it usually gets the job done and it’s easy to extend. (remember, the filesystem itself is a database, you don’t always need a relational database for everything)

I don’t think you should have two classes for a DB to connect, and a statement, rather something like below.

class Database
{
	
	public function __construct($host, $user, $name, $password)
	{
		// $this->DB = mysql_connect....
	}
	
	public function Query($strParams)
	{
		// return mysql_query($strParams);
	}
	
	public function Insert($strParams)
	{
	}
	
	public function Update($strParams)
	{
	}
	
	public function __destruct()
	{
		// mysql_disconnect();
	}
	
}

$DB = new DB('host', 'user', 'name', 'pass');
$DB->Query("SELECT * FROM someplace");

And a form example I suppose you could get use to something this way


class Form
{
	
	public function __construct()
	{
	}
	
	public function Create($type, $name, $value)
	{
		return "<input type='$type' name='$name' value='$value' />";
	}
}

$Form = new Form();
echo $Form->Create('text', 'name', 'John');


They are pretty simple examples but you’ll have to see what you like to do.
There are already Objects like PDO and MySQLi for Database handling but I think it’s better experience to start out writing your own little MySQL connector :stuck_out_tongue:

Both Joomla and Drupal use OOP. Before re-inventing the wheel download each and familiarize yourself with them. Though I use my own framework for certain projects, I still use each of these open source projects for applications to which they are well suited.

And if you ask around you’ll bump into fans of each on here.