1

I create my own mvc model, so I have to include all my models and controllers class.

For the moment each time I need a model in a controller I use:

require_once('myModel.php');

A little like import in java

But I have a question, is it a good solution to continue on this way ? I would prefer include all my models and controllers at the beginning of the code but I'm afraid this solution is too heavy.

Thanks

Ajouve
  • 9,735
  • 26
  • 90
  • 137
  • Why don’t you use MVC framework like cakephp, codeignator, Laravel. And why do you want to include all model unnecessarily. – Amar Banerjee Jun 21 '13 at 14:51
  • @AmarBanerjee, have you actually looked at how your mentioned frameworks implement autoloading? [Cake](https://github.com/cakephp/cakephp/blob/master/lib/Cake/Core/App.php#L534), [CI](https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Loader.php) and [Laravel](https://github.com/laravel/framework/blob/master/src/Illuminate/Support/ClassLoader.php). Even the less offensive on (laravel) is just a bunch of static functions wrapped in a namespace (that just happens to look like a class). Those are horrible pieces of code. You should not recommend them to newbies. – tereško Jun 22 '13 at 13:06

2 Answers2

3

Generally i would not recommend you to create your own framework, but if you insist or want to do it for learning purposes the PHP community created FIG (Framework Interop Group) to maintain consistency between frameworks, and how things should be handled.

The problem you are trying to solve is called autoloading, it's also documented AS PSR-0 standard, https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md, Have a nice read.

And here is PSR-0 compliant autoloader examples: PHP - most lightweight psr-0 compliant autoloader

Basically it means that your namescape path will tell where to look for file.

For example you want to get BussinessObject in bootstrap.php:

//MVC/Models/BussinessObject.php

namespace MVC/Models;

class BussinessObject {

}

//MVC/bootstrap.php

namespace MVC;

use MVC/Models/BussinessObject;

$BussinessObject = new BussinessObject()

Then autoloader will look for file in MVC/Models/BussinessObject.php, and calls require using that path.

Community
  • 1
  • 1
Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
  • Thanks for your answer, I tried Symfony2 but I don't realy like, I'll maybe test zend, have you a framework sugestion ? – Ajouve Jun 21 '13 at 15:24
  • It depends on your needs, Want something small go for micro framework like Silex. I am quite satisfied with Symphony, i would give a try for Laravel 4 looks like nice framework, Zend 2 is also better compared to 1, but learning it takes time. – Aurimas Ličkus Jun 21 '13 at 15:29
  • Note: Silex is not maintained anymore – Zoette Oct 15 '19 at 04:21
0

The best approach should be to include files only when they are really needed. As this isn't often practical, you should try to include on app init only the files containing the basic functions you use.

d.grassi84
  • 383
  • 1
  • 2
  • 11