开发者

PHP autoload MVC

I came across the __autoload function in PHP and would like to use it with my MVC folder structure. The function itsself is pretty easy, but how do I achieve a dynamic folder scanning after some kind of naming, please see example:

-application
--controller
--models
---entities
----house
---factories
----houseFactory
--views
-library
-public

As you maybe recognise its very close based on zend framework or other frameworks -as I come from those, however I would like to develop a website without framework and just started to write bootsrap file.

Maybe sombody could help me with the autoload in this - i think - advanced usage.

My Classnames will be like Model_Entities_H开发者_Python百科ouse or Model_Factory_HouseFactory

witch can be applied to the folder structure.


What I do mostly is use the SPL autoload function, which will help you to accomplish this quite easily. It should be something like this:

spl_autoload_register("MyClass::Autoloader");

Then, you can do something like this

class MyClass
{
  public static function Autoloader($className)
  {
    //parse $className and decide where to load from...
  }
}

If you´re using a naming convention, then you should be available to load the required file by just using the name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜