Error declaring an interface to the class in PHP?

Asked

Viewed 196 times

0

I’m having trouble declaring a interface to a classe in the PHP where I’m encapsulating the entire program with namespace. I created a interface calling for Yargle in the directory Cnab\Remessa and set the functions for that interface. Then I created a class called Filing cabinet in the directory Cnab\Remessa\Cnab240 where when trying to declare the interface to class, there is 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){}

}
  • If you are doing PDO? and follow a tutorial http://php.net/manual/en/class.pdo.php and do not use Pdo.

  • Using autoload? Your question needs more details. Are you using Poser? Are you using any custom autoload? Are the files included? How are you doing?

  • @Wallacemaxters, I am not using autoload, nor Composer. In this case it would be necessary to use these guys?

  • @Brunoduarte If you don’t want, you can use "include", but the project will be disorganized!

  • @Wallacemaxters yes true, recommend include or require is depends statement.

1 answer

1


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

A simple autoload example would be:

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

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

Another good way would be using the Composer in your projects.

With Composer it would basically declare the following code in your composer.json:

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

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

In this answer, I explain how to use Composer in Laravel, but the example can be followed to understand how it works (if you don’t use Laravel).

  • Wallace, using the first code as it would look? I would have to create a file for example: autoload.php in the root of the folder and then put this code inside and this file, is that it? And then how would it look, for me it works this way with autoload?

  • Yeah, if you call your whole logic on index.php, there is the place where he must stay.

Browser other questions tagged

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