views:

383

answers:

4

I've been using the CodeIgniter framework, however after learning Java and awesome features like Interfaces, Abstract classes, packages, and that PHP 5 also supports most of these features, I'm ready to graduate and build a real OO framework in PHP which uses all of these features (including namespaces), so I can build much more elegant designs.

What I'm thinking of is that rather than everything in the system sharing a single $this-> object like its done in CodeIgniter, e.g:

$this->load->model('box');
$this->box->something();

You would do the following to load the Box model and to call its something() method.

$box = new Framework\Models\Box();
$box->something();

Or the following

abstract class BaseController
{
   public function getModel($name)
   {
      $model = new Framework\Models\$model(); //Is this valid code?
      return $model;
   }
}

class MyController extends BaseController
{
    public function index()
    {
        $box = $this->getModel('box');
        $box->something();
   }
}

Are there any suggestions/pointers for building this, such as which minimum system classes I would need for the framework, what namespaces I should have, or any other features?

+4  A: 

A lot of experience is needed to build a technical framework (no offense) and there are already 1'000's of CMS/basic objects (session management, ORM, etc.) framworks/libraries.

Instead of wasting precious time (no offense again), you better have to:

  1. Evaluate and select the framework that better suits your needs (some have a really good OO architecture).
  2. Learn will using this good framework.
  3. Build your own OO business/application framework. (This is where you can learn, differentiate and create value).
Toto
Can you eleborate on #3?
Click Upvote
A framework with business logic only. Take stackoverflow: build/extend a framework to implement a "badge/reputation system" (for sure you will be successfull if you do it right and you will have fun). All the rules are "business" logic (it has value). How you save data in a DB, cookie, etc. leave it to the technical framework (it has no value). :)
Toto
+10  A: 

One thing I've noticed is that a framework is usually built for a specific purpose. Generic frameworks (like CodeIgniter) are good for smaller sites and getting things up and running quickly. However once you have specific things, which fall outside of the generic framework building your own does become a reality.

The only thing I would suggest is to be consistent. That is relentlessly consistent. If you decide to name things in camelCase then don't deviate from that. Or if you decide to use the NounVerb convention of naming methods i.e. spaceJump then don't switch to jumpSpace even if it 'sounds' better at the time.

Choose how you'll accept parameters and be consistent on that. Will you accept only parameters or associative arrays of parameters? Choose and stick with it.

I would also not over-engineer before you've written anything. The consistency thing will take you pretty far before you'll need to refactor... but I wouldn't be afraid to do that as well. (A unit test or two should ease those fears).

Yup, there's usually no right or wrong way to do things as long as you're consistent. Did I mention...

Be Consistent!

null
+4  A: 

You can build your own but why not make use of the vast libraries included with frameworks available and if required extend their functionality.

Why don't you take a look at the Zend framework.

It's fairly quick to get started and includes a lot of useful libraries and classes as standard. It's good for personal projects if your just looking to gain more experience with OOP.

http://framework.zend.com/

Andi
+3  A: 

I would look at Kohana. It came out of CodeIgnitor, and loading Models etc is done just the way you proposed.

Check out their core features, many of which relate directly to your question (I've highlighted those):

How is Kohana Different?

Although Kohana reuses many common design patterns and concepts, there are some things that make Kohana stand out:

  1. Community, not company, driven. Kohana development is driven by a team of dedicated people that need a framework for fast, powerful solutions.
  2. Strict PHP 5 OOP. Offers many benefits: visibility protection, automatic class loading, overloading, interfaces, abstracts, and singletons.
  3. Extremely lightweight. Kohana has no dependencies on PECL extensions or PEAR libraries. Large, monolithic libraries are avoided in favor of optimized solutions.
  4. GET, POST, COOKIE, and SESSION arrays all work as expected. Kohana does not limit your access to global data, but offers filtering and XSS protection.
  5. True auto-loading of classes. True on-demand loading of classes, as they are requested in your application.
  6. No namespace conflicts. All classes are suffixed to allow similar names between components, for a more coherent API.
  7. Cascading resources offer unparalleled extensibility. Almost every part of Kohana can be overloaded or extended without editing core system files. Modules allow multi-file plugins to be added to your application, transparently.
  8. Library drivers and API consistency. Libraries can use different "drivers" to handle different external APIs transparently. For example, multiple session storage options are available (database, cookie, and native), but the same interface is used for all of them. This allows new drivers to be developed for existing libraries, which keeps the API consistent and transparent.
  9. Powerful event handler. Observer-style event handlers allow for extreme levels of customization potential.
  10. Rapid development cycle. Rapid development results in faster response to user bugs and requests.
Chris Domigan