Error declaring an interface class in PHP?

0

I'm having trouble declaring a interface to classe in PHP where I'm encapsulating the entire program with namespace . I created a interface called IFile in the Cnab\Remessa directory and set the functions for that interface. Then I created a class named File in the Cnab\Remessa\Cnab240 directory where when trying to declare the interface to the class, an interface error not found.

INTERFACE

namespace Cnab\Remessa;

interface IArquivo {

     public function obter_cabecalho(array $parametros);

     public function obter_detalhes(array $parametros);

     public function grava_texto($nome_do_arquivo);

}  

CLASS

namespace Cnab\Remessa\Cnab240;

class Arquivo implements \Cnab\Remessa\IArquivo {

    public function obter_cabecalho(array $parametros){}

    public function obter_detalhes(array $parametros){}

    public function grava_texto($nome_do_arquivo){}

}
    
asked by anonymous 03.08.2016 / 14:02

1 answer

0

For you to use classes or interfaces the way you are using, you need a autoload . Because you are not including classes or interfaces through include .

A simple example of autoload would be:

spl_autoload_register(function () {
   include(__DIR__ . "/" . $pClassName . ".php");
});

So when you add, extend a class, or implement an interface, php would automatically execute the include of the files.

Another good way would be to use Composer in your projects.

With the composer would be basically declaring the following code in its composer.json :

"autoload": {
    "psr-4": {
         "Cnab\" : "pasta_base_do_namespace/",
    }  
}

Next, you should run the composer dump command to generate the autoload of your classes.

In this answer, I explain how to use Composer in Laravel, but the example can be followed to understand its operation (if you do not use Laravel).

03.08.2016 / 14:28