Jump to content

Content Management System design with OOP


DeadxBeat

Recommended Posts

Hello Everyone,

 

First off, I read the sticky, this is not a question about OOP but rather a question about design with OOP in mind.

 

With that said, lets get to it.

 

I'm a programmer with a good understanding of PHP and how to use it, I'm relatively new to using OOP but picked up on it quick. I'm trying to develop a content management system using OOP instead of just using an include file with a bunch of unorginized functions.

 

My question is a matter of opinion, I'm just looking for ideas on what classes I should make and for what.

 

So far, I've written the following classes down on a piece of paper:

 

 

Database Class(Load db variables, connect to db)

User Class(Add users, remove users, verify users, load user variables, session initiation)

News Class(Add news, delete news, modify news)

Admin Class(Modify User permissions)

Module Class(add module, remove module)

CMS Class(Display Navigation, Display News, Display Modules)

 

Do these seem like adequite classes with good functions also what else would you recommend adding to make a basic CMS. The thing I'm really stuck on is if I should use CMS Class to display everything or if i should include things like Display News in the news class and display modules in the module class.

 

Any ideas / sugguestions would be greatly appreciated.

 

Thanks, Daniel.

Link to comment
Share on other sites

Splitting up your code into modules is a good idea, you cannot go wrong doing this. Are you using a templating system such as Twig or Smarty?

 

I'm really stuck on is if I should use CMS Class to display everything or if i should include things like Display News in the news class and display modules in the module class.

 

I don't understand, show some code.

Link to comment
Share on other sites

No template, I want this all to be very basic and structured by myself.

 

What I meant by me being stuck is that I can't decide what functions should go where.

 

For example, right now in my main cms class i have a function called displayNews() and then in my news class is addNews() and remNews().

 

I'm not sure if I should put displayNews in the cms class or the news class because if i put it in the news class then I would have to create a new news object inside my main cms class and I assume that is bad practice.

 

So my question now, is it viable / good practice to call another classes function from within a different class?

Link to comment
Share on other sites

I didn't quite catch your class relations but could you extend your news class with your cms class?

For example (using the class names you gave):

class CMS
{
    public function displayNews()
    {
        // show the news
    }
}

class News extends CMS
{
    public function addNews()
    {
        // add news
    }
    public function remNews()
    {
        // remove news
    }
}

Now your News class will also have the displayNews method inherited from CMS. But this structure to me isn't really a good practice since CMS and News are in no way related to each other. You should have all your news related methods inside your News class. If you need those methods, instantiate your news class and use them.

Link to comment
Share on other sites

Alright, so just so I can clarify here is an example of what im doing.

 

Here is my class file:

<?php

require_once '_db.php';
require_once '_news.php';
require_once '_cms.php';
require_once '_user.php';
require_once '_admin.php';
require_once '_module.php';

$_db = new _db();
$_cms = new _cms();
$_news = new _news();
$_mods = new _module();

?>

 

then lets say my _news.php file looks like this:

<?php

    class _news {

        public function displayNews() {
            // display news
        }

    }

?>

 

then lets say my _cms.php file looks like this:

<?php

    class _cms {

        public function getPage($p = $_GET['p']) {
            if (isset($p)) {
                // display page function
            }
            else {
                $news = new _news();
                $news->displayNews();
            }

        }

    }

?>

 

This would be ok?

Link to comment
Share on other sites

A few notes for you to keep in mind:

 

If you are going to redistribute, or even if you're not it would be ideal to protect certain classes with the final key so you don't wind up overwriting methods in your core classes.

 

Funnily enough, I am working on the same thing and I have a protected core class with Init functions and Debug logging built right into them so I can debug modules as they are developed.  Then I have a public core class that ... well it does nothing at this point since I just started this yesterday and haven't gotten that far. My CMS will be more of a social based platform but I will provide you my source for that file so you can grasp an idea of what I mean.

 

When I was learning best practices, OOP, blah blah. I did get confused as to where to put my methods but if you have a class named Database, put your database methods there... Though, I would recommend, not because I prefer it myself, but because it is also OOP loading the MySQLi Class. That is, if you're wanting to learn the OOP. It helped me.

 

Here is the source of my run.class.php:

<?php
/**
* run.class.php
*/
/**
* Main class.
* Contains all necessary static methods to run the application.
* @method bool Init() Init( void) - Starts application
* @method resource Load_Module() Load_Module( str $module) - Loads passed module.
* @todo Add core error/debug logging and display. This will have to be built right into the core, not a module.
* @todo Module error/debug logging.
* @todo Core logging/debug levels.
* @todo Module logging/debug levels.
*/

final class NR {

 /**
  * @var mixed Sets debug On/Log/Off.
  * true = On (Print all messages to browser
  * 'log' = On (Log all messages to file/db?)
  * false = Off
  */
 var $debug = true;

 /**
  * Initializes the script.
  * @todo Load core modules after they are programmed, of course.
  * @static
  * @final
  */
final public static function Init() {
	self::Load_Module('core');
	self::Load_Module('db');
}

/**
 * Loads passed module.
 * @todo add Exception handling.
 * @param str $module The module to be loaded. Follows direcotry structure:
 * <pre> ROOT_DIR/module/$module/class/$module.class.php </pre>
 * @usage <code>
 * NR::Load_Module('user'); 
 * //Loads the user module class file, module/user/class/user.class.php
 * </code>
 * @static
 */
	final public static function Load_Module($module) {

	//Set up the directory string.
	$load = MOD_DIR . $module . DS .  MOD_CLASS_DIR .  $module . CLASS_FILE;
		$c = pathinfo($load);
		if (is_dir($c['dirname'])) {

			$dir = $c['dirname'];
			$filename = $dir . DS . $c['filename'] . '.' . $c['extension'];

				if (is_file($filename)) {
					echo $filename . ' is a file. Attempting to include file.<br />';
				}
		}
		else {
			return false;
		}
		if (include($filename)) {
			return true;	
		}

	 //Load
}

public static function Error($msg, $status = true, $code = '') {
	<!--snip--!>

}

 

I hope that I documented it well enough. Please do let me know. I plan on collaborating in the future and possibly redistributing. I'd like to document it as I go. It may take longer, but this way I don't have to go back and document it all.

 

Some of the stuff in there isn't modularized or logical yet, it is only an alpha version.

 

When I want to call LoadModule, I use the scope resolution operator since I am using static methods for my final class. I've defined some constants, etc.

 

NR::Module('some_module');

This loads the module according to how I've set up my directories (which in my case is SITE_ROOT/module/some_module/class/some_module.class.php). It's almost the same as just putting them all in one file, but easier to code. Again, I have a lot to program into it. Database 'drivers', other modules.

I also believe for dynamic loading of this type, providing a full path is less overhead than using your relative include and requires.

 

my class 'core' will have methods for settings, and everything that the app will need access to. Things that -must- not change, or are needed to load modules are built into my NR class, or the true core.. All or most of my modules will be an extension of 'core' and not NR.

I could probably go on about 5,000,000 things to take into consideration when planning an OOP project. The way I best learned was downloading and reading and trying to modify other scripts. I do have a few premium licenses to some scripts and those helped greatly in learning OOP and how it is organized.

 

I don't know 100% that what I'm doing is the semantic way, but it seems like the most logical based off of what I've learned so I can keep the entire app modular.

 

As far as your required classes that you might want to make necessary:

I'm not sure about User, what if you want to use it as a pure CMS with no registration? That could be something to consider down the road, maybe even be able to turn it on or off. That would require that you keep all your Admin and User methods separate which might get confusing.

 

You should code yourself a template module. It's a longer process, but it's extremely useful. For example: in each module folder I have I'm going to have a 'block' folder which will hold php files that are loaded by me template class. It will be the same idea as the NR::Load_Module, but it will be something along the lines of

$tpl->Load_Block($blockname);

each module will also include a template folder, holding any actual pages that need rendering and will be called in a similar manner.

In the total scheme of OOP projects having module based templates does make things much faster after your core development is finished.

 

...new news object inside my main cms class and I assume that is bad practice.
static methods and the '::' operator helps there but put your news methods in the news class as johnny86 said.

 

Geeze, my fingers are just going off on the keyboard. I think I had better stop here lest I confuse you or give you misinformation. I can help you with your understanding of how to plan out an OOP project if you like, just message me and I'll be more than happy to help. :)

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.