CodeIgniter Design Paterns |
[eluser]Lazos[/eluser]
Hi. I am doing my dissertation and I have to use design patterns and a framework for developing a CMS. As for framework I am using CodeIgniter because it has great User Guide and very easy to use. But I also have to use some design patterns and I am kind of lost. I try to read about them from books but still I do not really get it... Can you post a link with simple PHP design patterns examples and also I would like to know what kind of design pattern is each class of CodeIgniter or at least some basic classes? I am running out of time. I would be grateful if you answer me these questions. Thanks.
[eluser]Renard Urbain[/eluser]
There are two design patterns that are very easy to implements in PHP : Registry and Observer. Code Igniter uses the Model-View-Controller pattern itself. If you want to know more about the registry pattern I wrote a blog post about it a while ago: http://labs.rickypoo.net/2008/10/04/desi...-registry/ For the observer it's quite simple actually. Say for instance you want to build a validation class: <?php class validation { protected $_validations = array(); public function register($validation) { $this->_validation = array_push($validation); } public function validate() { foreach($this->_validations as $validation) { $result = $validation->validate(); if($result == false) return false; } return true; } } class validation_is_number { protected $_item; public function __construct($item) { $this->_item = $item; } public function validate() { return is_int($this->_item); } } ?> and the implementation: <?php $validation = new validation(); $validation->register(new validation_is_number($_POST['range_1']); $validation->register(new validation_is_number($_POST['range_2']); $result = $validation->validate(); ?> So basically, with the observer, you have a class, that implements usually an interface. Each class have one method in common which is used to be notified of something. The hook system in code igniter is one example too of the observer pattern. There are tons of common design pattern used in PHP but here is a quick introductory with IBM that might help you too : http://www.ibm.com/developerworks/librar...signptrns/
[eluser]Rey Philip Regis[/eluser]
Hi Lazos, Well, the problem you're having right now is experience. You said that even you read books on patterns really can't get it. Actually when you are experienced developer, so you see the patterns you need, cause patterns are just theories on how to solve problems or theories on how to make your code more flexible manageable and extensible, that's why there are no syntax on how to do a pattern, just remember what the pattern does. And patterns are solutions to the problems that past developers encountered. There are some patterns I suggest you to look and remember them, cause what I'll give you are the basics and the most common patterns used in most projects, not just me but almost all developers. 1. Factory pattern 2. Singleton pattern 3. MVC pattern (Model-View-Controller) 4. Active Record Pattern 4. Registry Pattern 5. Adapter Pattern 6. Two Step View Pattern (Using views) Hope this patterns helps you building your CMS. Good day.
[eluser]Hannes Nevalainen[/eluser]
Most of CI standard classes are implemented use the singleton pattern.
[eluser]Lazos[/eluser]
[quote author="Rey Philip Regis" date="1226844931"]Hi Lazos, Well, the problem you're having right now is experience. You said that even you read books on patterns really can't get it. Actually when you are experienced developer, so you see the patterns you need, cause patterns are just theories on how to solve problems or theories on how to make your code more flexible manageable and extensible, that's why there are no syntax on how to do a pattern, just remember what the pattern does. And patterns are solutions to the problems that past developers encountered. There are some patterns I suggest you to look and remember them, cause what I'll give you are the basics and the most common patterns used in most projects, not just me but almost all developers. 1. Factory pattern 2. Singleton pattern 3. MVC pattern (Model-View-Controller) 4. Active Record Pattern 4. Registry Pattern 5. Adapter Pattern 6. Two Step View Pattern (Using views) Hope this patterns helps you building your CMS. Good day.[/quote] About the experience you are right. I do not have that much experience. Anyway in my mind for example if I want to build a logger I wont use the Observer pattern if is the appropriate for such a situation. For me the answer to such a problem will be the use of a function that I pass some parameters and then saving the data to the DB using that function or other functions. I did not know that MVC and Active record are Design patterns... anyway I used both of them until now. The Singleton pattern I only see it at the CI_BASE file. Only one instance of the super object CI is called. Registry pattern from what I have read is almost the same as singleton... Factory and adapter again I just read them but without experience I could not apply them. Two steps view pattern I will check it out. About the Fluffy Cat page I visited it 1000 times... ![]() Anyway my WCMS is almost finish now. The next 3 4 days I will try and change some parts of the code and use design patterns or something. Anyway thanks so much for your posts. |
Welcome Guest, Not a member yet? Register Sign In |