4

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_House or Model_Factory_HouseFactory

witch can be applied to the folder structure.

1
  • have you considered cycling through all of the folders to check if the file exists, rather than parsing a string to find the path? Commented Jan 22, 2011 at 1:54

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, looks good and very professional, but creating a whole new class leaves a feeling of "too much". Anyway, it's probably the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.