Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a real object oriented framework in PHP, suggestions wanted

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?

like image 612
Ali Avatar asked Nov 29 '22 20:11

Ali


2 Answers

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!

like image 142
null Avatar answered Dec 01 '22 09:12

null


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).
like image 23
Toto Avatar answered Dec 01 '22 09:12

Toto