Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter design patterns

Its been couple of years I have been working on Codeigniter and I just wanted to check design patterns. I wanted to implement different design patterns on my working projects so I could understand these stuffs in a better way.

I know Codeigniter is following MVC pattern, but which design pattern is codeigniter following ?

Can we say a database.php, a database class is implementing a singleton design pattern ? I am saying since as far as I percieved, on singleton, a single instance is created which provides a global access and that is what CI database config object does.

like image 623
hsuk Avatar asked Jan 31 '13 06:01

hsuk


People also ask

What are PHP Design Patterns?

PHP Design patterns is an Object-Oriented Programming (OOP) concept that is now also used in Drupal 9 projects. With Drupal's adoption of modern PHP and OOP concepts since version 8, design patterns can be leveraged for cleaner and more robust programming.

What is C++ design pattern?

Patterns are commonly found in objected-oriented programming languages like C++ or Java. They can be seen as a template for how to solve a problem that occurs in many different situations or applications. It is not code reuse, as it usually does not specify code, but code can be easily created from a design pattern.

What is design pattern in Scala?

Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system. The classical design patterns are the 23 design patterns by GoF. This project implements dozens of design patterns in Scala.

What is CodeIgniter used for?

CodeIgniter is an open-source software rapid development web framework, for use in building dynamic web sites with PHP.


1 Answers

Yes, Codeigniter's loader currently follows the singleton pattern, or at least that's the pattern that most accurately describes it. When you execute the following:

$this->load->library('foo');
$this->load->model('foo');
$this->load->database('foo');

The loader does the following things:

  • Check to see if the class you're loading has been loaded previously by checking a registry of loaded classes. Silently ignore the request with a debug log entry if it has been loaded.

  • Instantiate the class with whatever parameters you set, create a reference to that object within the framework (singleton-ish) super object named after the class, or whatever custom name you pass. Store the reference, ignore subsequent attempts to load.

On bootstrap, the magic functions in the global scope behind the loader methods are used to construct the DB, core libraries, etc.

A more traditional singleton approach would do something like this (with auto loading):

return $className::instance();

... Where the instance method would return an instance, or construct if not yet instantiated, thus obviating any need to keep track of what has or has not been loaded. If the class has been loaded, a reference would be passed, otherwise a new object would be created and returned.

I suppose technically, CI is its own pattern in this regard, but close enough to a singleton that the term does accurately apply. It really is a singleton, just not implemented in a typical way.

At last I checked there were patches floating around for CI-3 that makes the loader much more flexible, allowing one to work outside of the super object as they please by returning an object or reference in these cases, but I don't know the status of Ellis Labs taking them.

like image 101
Tim Post Avatar answered Sep 18 '22 13:09

Tim Post