Error importing class with use

0

The structure of my folders and files looks like this:

WhatI'mtryingtodowithoutsuccessistousetheValidation.phpclassinUser.php

ForthisIusedthecommanduseClasses\Validacao;

User.php

Validation.php

ButIalwaysgetthiserror:

I'm using the slim framework

    
asked by anonymous 05.06.2018 / 18:25

1 answer

4

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

The composer (which Slim uses) makes use of spl_autoload that it does "program" your scripts to find the classes, in case the composer uses composer-autoload , so you have to add it to your composer.json , like this:

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

The Classes\ is the prefix to identify by the namespace, and "Classes" is the folder where they are located, it 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 composer update command if you want to upgrade the classes as well as download the dependencies of Slim and other packages you've added

So it will be available in composer-autoload

Read more about composer

The basics:

Some useful links in Portuguese for answers that I have formulated:

05.06.2018 / 18:29