How to use Composer to autoload classes from outside the vendor?

The src directory would be in your project root. Its on the same level as vendor directory is. If you define “autoload”: { “psr-4”: { “DG\\Munchkin\\”: “src/DG/Munch/” } } this will not load classes from /var/www/html/xxx/vendor/yyy/src/DG/Munch, like you stated. Because your project structure is: /var/www/html/ +- /xxx (project) – composer.json +- /src +- DG +- … Read more

php spl_autoload_register vs __autoload?

__autoload is generally considered obsolete. It only allows for a single autoloader. Generally you should only use __autoload if you’re using a version of PHP without support for spl_autload_register. spl_autoload_register allows several autoloaders to be registered which will be run through in turn until a matching class/interface/trait is found and loaded, or until all autoloading … Read more

What is autoload in php? [duplicate]

This will be of help to you about usage of autoload. http://ditio.net/2008/11/13/php-autoload-best-practices/ It’s a magic function that helps you include / require files using class name. function __autoload($class_name) { require_once $DOCUMENT_ROOT . “/classes/” . $class_name .“.php”; } It’s deprecated at PHP 7.2.0 and spl_autoload_register is recommended for that purpose.

PHP – most lightweight psr-0 compliant autoloader

You ask extremely lightweight, let’s do so 😉 Timothy Boronczyk wrote a nice minimal SPL autoloader : http://zaemis.blogspot.fr/2012/05/writing-minimal-psr-0-autoloader.html I condensed the code like this: function autoload1( $class ) { preg_match(‘/^(.+)?([^\\\\]+)$/U’, ltrim( $class, ‘\\’ ), $match ) ); require str_replace( ‘\\’, “https://stackoverflow.com/”, $match[ 1 ] ) . str_replace( [ ‘\\’, ‘_’ ], “https://stackoverflow.com/”, $match[ 2 ] … Read more

PHP adding custom namespace using autoloader from composer

You actually don’t need custom autoloader, you can use PSR-4. Update your autoload section in composer.json: “autoload”: { “psr-4”: { “Classes\\Weather\\”: “Classes/CronJobs/Weather” } } To explain: it’s {“Namespace\\\”: “directory to be found in”} Don’t forget to run composer dump-autoload to update Composer cache. Then you can use it like this: include(LIBRARY .’autoload.php’); $weather = new … Read more

Using Composer’s Autoload

Every package should be responsible for autoloading itself, what are you trying to achieve with autoloading classes that are out of the package you define? One workaround if it’s for your application itself is to add a namespace to the loader instance, something like this: <?php $loader = require ‘vendor/autoload.php’; $loader->add(‘AppName’, __DIR__.’/../src/’);

Autoload classes from different folders

I see you are using controller_***** and model_***** as a class naming convention. I read a fantastic article, which suggests an alternative naming convention using php’s namespace. I love this solution because it doesn’t matter where I put my classes. The __autoload will find it no matter where it is in my file structure. It … Read more

Loading view outside view folder with CodeIgniter

Don’t know whether it’s a correct way, but it works 🙂 In your application/core folder put this loader extention <?php class MY_Loader extends CI_Loader { function ext_view($folder, $view, $vars = array(), $return = FALSE) { $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH . $folder . “https://stackoverflow.com/” => TRUE)); return $this->_ci_load(array( ‘_ci_view’ => $view, ‘_ci_vars’ => $this->_ci_object_to_array($vars), ‘_ci_return’ => … Read more