Disable composer require with php

2

I have an example of JSON :

{
    "name": "xxxxxx",
    "description": "xxxxx",
    "type": "beta",
    "license": "MITS",
    "authors": 
    [
        {
            "name": "Leonardo Vilarinho",
            "email": "[email protected]"
        }
    ],
    "require": 
    {
        "php": ">=5.5.12",
        "twig/twig" : "*",
        "respect/validation" : "*"
    },
    "require-dev" : 
    {
        "phpunit/phpunit" : "*"
    },
    "config": 
    {
        "vendor-dir": "libs"
    }
}

I'll probably have more libs in my project, want to know if there's any way I can disable and enable these libs through PHP? If so, how can I do this? (I'm a layman when it comes to composer , so I can not even try anything, the searches did not lead me to anything).

Even though they are downloaded in the project, they are not loaded into autoload of composer .

    
asked by anonymous 11.07.2016 / 02:23

1 answer

2

The composer-autoloader uses spl_autoload_register , classes that are in "require": are only loaded if using new , or static calls Exemplo::teste(); execute includes, it works like this:

If you do this the class Foo\Bar\Baz is not is loaded:

<?php
use Foo\Bar\Baz;

require_once 'vendor/autoload.php';

If you do this the class Foo\Bar\Baz is loaded, at the moment you use new :

<?php
use Foo\Bar\Baz;

require_once 'vendor/autoload.php';

//Essa linha dispara o spl_autoload e inclui o ./src/Foo/Bar/Baz.php
$baz = new Baz;

Or:

<?php
require_once 'vendor/autoload.php';

//Essa linha dispara o spl_autoload e inclui o ./src/Foo/Bar/Baz.php
$baz = new Foo\Bar\Baz;

If you do this the Foo\Bar\Baz class is loaded the moment you call the exemplo method:

<?php
use Foo\Bar\Baz;

require_once 'vendor/autoload.php';

//Essa linha dispara o spl_autoload e inclui o ./src/Foo/Bar/Baz.php
Baz::exemplo();

Extending classes also triggers autoload, even if you do not use the child class:

<?php
use Foo\Bar\Baz;

require_once 'vendor/autoload.php';

//Essa linha dispara o spl_autoload e inclui o ./src/Foo/Bar/Baz.php
class Exemplo extends Baz {
}

How does spl_autoload_register

  • It only runs if the class does not exist yet
  • If you use use does not fire and does not include anything, use is more for creating aliases (nicknames)

I recommend you read:

11.07.2016 / 02:53