Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer autoload full example?

I am trying to put all the peaces together I found about autoloading a class in composer but I can't make it work. Every example I see is missing some part. Basically it comes down to two files with 4 lines:

index.php

$loader = require 'vendor/autoload.php';
$loader->add('Vendor\\', __DIR__.'/../app/');
new Vendor_Package_Obj();

app/Vendor/Package/Obj.php

class Obj {}

I also tried psr-4 and all thinkable combinations of folders and names for `Vendor Package Obj? but no luck finding a working solution.

How can I autoload a file with composer using any of those standards?

like image 680
PiTheNumber Avatar asked Jan 20 '15 13:01

PiTheNumber


People also ask

How do I use autoload in Composer?

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.

How does autoload work in PHP?

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.

What does Composer dump-autoload do?

Composer dump-autoload: The composer dump-autoload will not download any new thing, all it does is looking for all the classes and files it needs to include again.

What is Classmap in Composer json?

Classmap# The classmap references are all combined, during install/update, into a single key => value array which may be found in the generated file vendor/composer/autoload_classmap. php . This map is built by scanning for classes in all .


1 Answers

According to PSR-4, The fully qualified class name MUST have a top-level namespace name, also known as a "vendor namespace" and underscores have no special meaning in any portion of the fully qualified class name.

Try this:

cd ~
mkdir -p testproj/src/MyApp/Package
cd testproj
composer init && composer update

Create your index.php with this content:

<?php
$loader = require 'vendor/autoload.php';
$loader->add('MyApp\\', __DIR__.'/src/');
$a = new MyApp\Package\Obj();
var_dump($a);

And put the Obj class (src/MyApp/Package/Obj.php) :

<?php
namespace MyApp\Package;

class Obj
{}

Now when you run the code:

php index.php

You should get this as output:

class MyApp\Package\Obj#2 (0) {
}

Also directory scaffolding should look like this:

testproj
├── composer.json
├── index.php
├── src
│   └── MyApp
│       └── Package
│           └── Obj.php
└── vendor
    ├── autoload.php
    └── composer
        ├── ClassLoader.php
        ├── autoload_classmap.php
        ├── autoload_namespaces.php
        ├── autoload_psr4.php
        └── autoload_real.php
like image 126
edigu Avatar answered Sep 24 '22 02:09

edigu