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/');
How to use Composer's autoloader to autoload arbitrary classes/namespaces not configured in composer.json?
With this file structure:
Where class Test1
is:
// out/Test1.php
class Test1
{
public function foo(): string
{
return "foo bar baz";
}
}
and class FooBar/Console/Test2
is:
// psr4/someroot/Console/Test2
namespace FooBar\Console;
class Test2
{
public function bar(): string
{
return "fizz buzz";
}
}
and having this on the test script (test.php
)
// test.php
require __DIR__ . '/vendor/autoload.php';
$loader = new \Composer\Autoload\ClassLoader();
$loader->add('Test1', __DIR__ . '/out');
$loader->addPsr4('FooBar\\', __DIR__ . '/psr4/someroot');
$loader->register();
$t1 = new Test1();
$t2 = new \FooBar\Console\Test2();
echo $t1->foo(), "\n";
echo $t2->bar(), "\n";
I can run php test.php
perfectly fine.
So something else must be misconfigured in your project. Use the above example as a guide to fix it.
How to use multiple autoload functions for composer?
I want to use vendor/autoload.php - for composer files, but only my autoload-my-func.php for classes with my custom namespace. (My_Custom_Namespace)
You should use composer's auto loader for both and define your custom namespace in your composer.json file. If you are stuck with a poorly designed legacy system, and you need something to bridge a gulf of stupidity until you can create a better solution, then you should take a look at the documentation for spl_autoload_register. Specifically, the line about "if there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined." When you include composers autoload.php, you eventually get a few calls to spl_autoload_register(), so simply register your custom autoloader after including composer's.
Class not found when trying to use composer auto load in a wordpress plugin
After some research, I discovered solution. Adding:
"config": {
"optimize-autoloader": true
}
to composer.json
solved the issue.
Custom autoload conflicts with Composer's autoload?
You might be able to replace your custom autoloader function with a classmap
autoloader in your composer.json
:
{
"autoload": {
"classmap": ["classes/"],
"files": ["custom_funcs.php"]
}
}
This map is built by scanning for classes in all
.php
and.inc
files in the given directories/files.
Whenever you create a new class, you might have to update composer's autoloader by using composer dump-autoload
for it to be picked up. I am not sure if this is the case by default or only when optimizing the autoloader, though. In any case this could be solved by adopting PSR-0 or PSR-4 naming conventions for new classes.
edit: Since all files will be autoloaded for you with composer's autoloader, it should not be necessary anymore to require
them manually. Therefore you could also remove the setupBeforeClass
-method
composer autoload in laravel
Every package should be responsible for autoloading itself
Consider Laravel as a single package like others
You can find vendor composer.json
files in each packages separately
Here are the directory structure
<web-root>
|--------src/
| |--------App/
| |
| |--------Test/
|
|---------library/
|
|---------vendor/
| |
| |---------composer/
| | |---------autoload_psr4.php
| |
| |----------autoload.php
|
|-----------composer.json
|
You can take a look my another answer for this autoload classmap
Cannot autoload class using composer from within other autoloaded class file
You're not using a FQCN of the Config
class, but a name relative to App\Database
namespace.
To fix this, you need to prefix the Config
class with a \
:
\App\Config\Config::GetDatabaseConfig();
or even better, you can import the Config
class with use App\Config\Config;
and then use it with Config::GetDatabaseConfig()
Composer autoload not working for package with custom installer-paths set
I overlooked something, and this turned out not to be a problem with composer. The path to the vendor directory was not the same on my local disk vs inside the docker container.
Related Topics
PHP Best Practices for User Authentication and Password Security
Redirecting from Http to Https with PHP
Installing Mcrypt Extension for PHP on Osx Mountain Lion
PHP Date Add 5 Year to Current Date
Adding 30 Minutes to Datetime PHP/Mysql
Apply a Coupon Programmatically in Woocommerce
Increasing PHP Memory_Limit. at What Point Does It Become Insane
Which Compression Method to Use in PHP
Storing Datetime as Utc in PHP/Mysql
Setting a PHP $_Session['Var'] Using Jquery
Is_Null($X) VS $X === Null in PHP
How to Inject a Repository into a Service in Symfony