DeadxBeat Posted February 1, 2011 Share Posted February 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226369-content-management-system-design-with-oop/ Share on other sites More sharing options...
DeadxBeat Posted February 5, 2011 Author Share Posted February 5, 2011 Would appreciate a reply from anyone with experience. Quote Link to comment https://forums.phpfreaks.com/topic/226369-content-management-system-design-with-oop/#findComment-1170393 Share on other sites More sharing options...
lastkarrde Posted February 5, 2011 Share Posted February 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226369-content-management-system-design-with-oop/#findComment-1170475 Share on other sites More sharing options...
DeadxBeat Posted February 6, 2011 Author Share Posted February 6, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/226369-content-management-system-design-with-oop/#findComment-1170504 Share on other sites More sharing options...
johnny86 Posted February 6, 2011 Share Posted February 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226369-content-management-system-design-with-oop/#findComment-1170520 Share on other sites More sharing options...
DeadxBeat Posted February 6, 2011 Author Share Posted February 6, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/226369-content-management-system-design-with-oop/#findComment-1170528 Share on other sites More sharing options...
ignace Posted February 6, 2011 Share Posted February 6, 2011 public function getPage($p = $_GET['p']) Does not work as you are only allowed to specify static values. This would be ok? Depends on your opinion versus standards and best practices. Quote Link to comment https://forums.phpfreaks.com/topic/226369-content-management-system-design-with-oop/#findComment-1170610 Share on other sites More sharing options...
pornophobic Posted February 6, 2011 Share Posted February 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226369-content-management-system-design-with-oop/#findComment-1170662 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.