Error importing class with use

Asked

Viewed 150 times

0

The structure of my folders and files is like this:

inserir a descrição da imagem aqui

What I am trying to do without success, is to use the class Validacao.php in the User.php

For that I used the command use Classes\Validacao;

User.php

inserir a descrição da imagem aqui

Validation.php

inserir a descrição da imagem aqui

But I always get this mistake:

inserir a descrição da imagem aqui

I’m using the slim framework

  • How you are declaring the Validation class call?

1 answer

4


This "class" needs to be in the composer.json, PHP cannot guess where it is located and the use is not equal to include as I explained in:

The Composer (which Slim uses), makes use of the spl_autoload that this yes "program" its scripts to locate the classes, in this case Composer uses the composer-autoload, then you need to add it to your composer.json, thus:

"autoload": {
    "psr-4": {
        "Slim\\": "Slim",
        "Classes\\": "Classes"
    }
},

The Classes\\ is the prefix to be identified by the namespace, and "Classes" is the folder where they are located, should look similar to this:

{
    "name": "slim/slim",
    "type": "library",
    "description": "Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs",
    "keywords": ["framework","micro","api","router"],
    "homepage": "https://slimframework.com",
    "license": "MIT",
    "authors": [
        {
            "name": "Josh Lockhart",
            "email": "[email protected]",
            "homepage": "https://joshlockhart.com"
        },
        {
            "name": "Andrew Smith",
            "email": "[email protected]",
            "homepage": "http://silentworks.co.uk"
        },
        {
            "name": "Rob Allen",
            "email": "[email protected]",
            "homepage": "http://akrabat.com"
        },
        {
            "name": "Gabriel Manricks",
            "email": "[email protected]",
            "homepage": "http://gabrielmanricks.com"
        }
    ],
    "require": {
        "php": ">=5.5.0",
        "pimple/pimple": "^3.0",
        "psr/http-message": "^1.0",
        "nikic/fast-route": "^1.0",
        "container-interop/container-interop": "^1.2",
        "psr/container": "^1.0"
    },
    "require-dev": {
        "squizlabs/php_codesniffer": "^2.5",
        "phpunit/phpunit": "^4.0"
    },
    "provide": {
        "psr/http-message-implementation": "1.0"
    },
    "autoload": {
        "psr-4": {
            "Slim\\": "Slim",
            "Classes\\": "Classes"
        }
    },
    "scripts": {
        "test": [
            "@phpunit",
            "@phpcs"
        ],
        "phpunit": "php vendor/bin/phpunit",
        "phpcs": "php vendor/bin/phpcs"
    }
}

After adding in Composer.json run the command:

composer dump

Or run the command composer update if you want that in addition to updating the classes it also download the dependencies of Slim and other packages that you added

This way it will be available on Poser-autoload


Read more about Composer

The basic:

Some useful links in Portuguese of answers I wrote:

  • Exactly that!!! Thank you very much

  • 2

    If you just want to make Composer generate class autoloaders, don’t use composer update, but only composer dump. The composer update will scan for new versions, the composer dump will just work on autoloads

  • @Wallacemaxters opaa!!! True, updating!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.