Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way To Autoload Classes In PHP

I'm working on a project whereby I have the following file structure:

index.php |---lib |--|lib|type|class_name.php |--|lib|size|example_class.php 

I'd like to auto load the classes, class_name and example_class (named the same as the PHP classes), so that in index.php the classes would already be instantiated so I could do:

$class_name->getPrivateParam('name'); 

I've had a look on the net but can't quite find the right answer - can anyone help me out?

EDIT

Thanks for the replies. Let me expand on my scenario. I'm trying to write a WordPress plugin that can be dropped into a project and additional functionality added by dropping a class into a folder 'functionality' for example, inside the plugin. There will never be 1000 classes, at a push maybe 10?

I could write a method to iterate through the folder structure of the 'lib' folder, including every class then assigning it to a variable (of the class name), but didn't think that was a very efficient way to do it but it perhaps seems that's the best way to achieve what I need?

like image 967
Sjwdavies Avatar asked Jul 23 '13 09:07

Sjwdavies


People also ask

How do you autoload in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

How does PHP autoload work?

The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path. File names don't need to obey any convention. All files are searched for class definitions.

How autoload PHP class the composer way?

After you create the composer. json file in your project root with the above contents, you just need to run the composer dump-autoload command to create the necessary autoloader files. These will be created under the vendor directory. Finally, you need to include the require 'vendor/autoload.

What is an autoloader in PHP?

Autoloading is the process of automatically loading PHP classes without explicitly loading them with the require() , require_once() , include() , or include_once() functions. It's necessary to name your class files exactly the same as your classes. The class Views would be placed in Views.


2 Answers

Please, if you need to autoload classes - use the namespaces and class names conventions with SPL autoload, it will save your time for refactoring. And of course, you will need to instantiate every class as an object. Thank you.

Like in this thread: PHP Autoloading in Namespaces

But if you want a complex workaround, please take a look at Symfony's autoload class: https://github.com/symfony/ClassLoader/blob/master/ClassLoader.php

Or like this (I did it in one of my projects):

<? spl_autoload_register(function($className) {     $namespace=str_replace("\\","/",__NAMESPACE__);     $className=str_replace("\\","/",$className);     $class=CORE_PATH."/classes/".(empty($namespace)?"":$namespace."/")."{$className}.class.php";     include_once($class); }); ?> 

and then you can instantiate your class like this:

<? $example=new NS1\NS2\ExampleClass($exampleConstructParam); ?> 

and this is your class (found in /NS1/NS2/ExampleClass.class.php):

<? namespace NS1\NS2 {     class Symbols extends \DB\Table     {         public function __construct($param)         {             echo "hello!";         }     } } ?> 
like image 80
Serge Velikan Avatar answered Oct 23 '22 09:10

Serge Velikan


If you have an access to the command line, you can try it with composer in the classMap section with something like this:

{     "autoload": {         "classmap": ["yourpath/", "anotherpath/"]     } } 

then you have a wordpress plugin to enable composer in the wordpress cli : http://wordpress.org/plugins/composer/

like image 40
lilobase Avatar answered Oct 23 '22 10:10

lilobase