8

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?

Community
  • 1
  • 1
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • Your class is not called `Vendor_Package_Obj`. It's called `Obj` and should be declared in a namespace. – deceze Jan 20 '15 at 13:09
  • Some examples use namespaces, other doesn't. Most example do not show Obj.php at all. Just for once give a full example. It's only a few lines... – PiTheNumber Jan 20 '15 at 13:12

1 Answers1

14

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
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
edigu
  • 9,878
  • 5
  • 57
  • 80
  • This worked after I deleted all other `Obj.php` and folders I had in `src`. Thanks a lot! – PiTheNumber Jan 20 '15 at 13:24
  • @edigu can you help me with this one ? => http://stackoverflow.com/questions/42179503/why-am-i-getting-error-even-after-declaring-namespaces-correctly?noredirect=1#comment71522376_42179503 – Akshay Shrivastav Feb 12 '17 at 01:49
  • A very nice example to use as a template. – Mike Mar 18 '18 at 21:32
  • @edigu Does the code of `index.php` need to be put at each file which is used as page? E.g. index.php, faq.php, about.php – RoestVrijStaal Jun 27 '18 at 08:37
  • 1
    If your app does not have a router or implements a kind of front controller yes. I strongly recommend having a router instead of creating separate files such as faq.php and about.php You can take a look into nikic/fastroute on GitHub for a good, framework independent example. – edigu Jun 27 '18 at 09:21