namespace php7 does not find url?

Asked

Viewed 121 times

0

I’m trying to learn namespace but I’m having a hard time.

Even doing exactly as it is in a video on youtube, but it doesn’t work.

I have the project folder

Projeto
Projeto\Cadastros
Projeto\Cadastros\Index.php
Projeto\Cadastros\Pessoa
Projeto\Cadastros\Pessoa\Pessoa.php
Projeto\Cadastros\Teste
Projeto\Cadastros\Teste\Pessoa.php

Index.php

<?php

 ini_set("display_errors",true);
 ini_set("display_startup_erros",1);
 error_reporting(E_ALL && E_NOTICE);
 error_reporting( E_ALL | E_STRICT ); // PHP 5.3
 error_reporting( E_ALL ); // Todas as outras versões 

 $pessoa = new Pessoa(1, 40, "Carlos");

 $pessoa2 = new Pessoa2();
 $pessoa2->setIdPessoa(1);
 $pessoa2->setIdadePessoa(43);
 $pessoa2->setNomePessoa("Cleonice");


 echo $pessoa->getIdPessoa()."<br >";
 echo $pessoa->getNome()."<br >";
 echo $pessoa->getIdadePessoa()."<br >";

 echo "<br />";

 echo $pessoa2->getIdPessoa()."<br >";
 echo $pessoa2->getNomePessoa()."<br >";
 echo $pessoa2->getIdadePessoa()."<br >"; 


?>

Pessoa.php

<?php

namespace Cadastros\Pessoa\Pessoa;

Class Pessoa {

    private $idPessoa;
    private $idadePessoa;
    private $nome;

    public function __construct ($_idPessoa, $_idadePessoa, $_nome) {
        $this->idPessoa = $_idPessoa;
        $this->idadePessoa = $_idadePessoa;
        $this->nome = $_nome;
    }

    public function getIdPessoa () {
        return $this->idPessoa;
    }

    public function getIdadePessoa () {
        return $this->idadePessoa;
    }

    public function getNome () {
        return $this->nome;
    }
}


?>

Pessoa.php

<?php

namespace Cadastros\Teste\Pessoa;

Class Pessoa {

    private $idPessoa;
    private $idadePessoa;
    private $nomePessoa;

    public function __construct () {}


    public function setIdPessoa ($_idPessoa) {
        $this->idPessoa = $_idPessoa;
    }   

    public function setIdadePessoa ($_idadePessoa) {
        $this->idadePessoa = $_idadePessoa;
    }   

    public function setNomePessoa ($_nomePessoa) {
        $this->nomePessoa = $_nomePessoa;
    }

    public function getIdPessoa () {
        return $this->idPessoa;
    }

    public function getIdadePessoa () {
        return $this->idadePessoa;
    }

    public function getNomePessoa () {
        return $this->nomePessoa;
    }
}


?>

Error

Fatal error: Uncaught Error: Class 'Pessoa' not found in C:\Program Files\Apache24\Apache24\htdocs\funerariasaopedro.net.br\Cadastros\index.php:19 Stack trace: #0 {main} thrown in C:\Program Files\Apache24\Apache24\htdocs\funerariasaopedro.net.br\Cadastros\index.php on line 19

What’s wrong?

  • Is use Person Person Person Registrations;

  • Not found too!

  • Besides the namespace is wrong, is talking include the file!

  • Check it out. Besides having the namespace you still need to do the require_once ""? The namespace doesn’t do that? So the namespace is just to do duplicate work. If I still need to do the file include, then just do include. Or did I misunderstand the concept of namespace?

  • Your namespace I would do so for example in the class Person would put namespace Projeto\Cadastros and would use use Projeto\Cadastros\Pessoa and need to work with include, require or even an auto_load, have plenty of answers about it here on the site just search!

  • http://php.net/manual/en/language.oop5.autoload.php e https://answall.com/questions/tagged/php-autoload

  • in other words: do 2 jobs in one: include and namespace. Unless I got it wrong! Thanks!

Show 2 more comments

1 answer

0

Your PHP just doesn’t do anything, use it’s not like import java or using of C#, he is not a include, it is only a part of the language to reference a "namespace" with a nickname.

As I explained in:

The right thing would be to use spl_autoload_register (or other functions of the SPL for this purpose), as I have already replied in:


Now if you’re using composer (i said "if you are"), so you should include Autopload in your index.php, because only then will he be able to identify the classes by the namespaces and solve them, of course it is necessary to configure the composer.json also, as I have already replied in:

If you want to use Poser, then read more about it at:


[Extra] about errors in PHP

This part here is probably wrong:

 ini_set("display_errors",true);
 ini_set("display_startup_erros",1);
 error_reporting(E_ALL && E_NOTICE);
 error_reporting( E_ALL | E_STRICT ); // PHP 5.3
 error_reporting( E_ALL ); // Todas as outras versões 

Simply setting up in php.ini would already solve it, if it’s production then don’t use it, just for development, besides display_startup_erros really has a different intention than you are probably imagining and applying error_reporting will have no effect, apart from the purpose of the error_reporting is much more than displaying errors, read this:

Browser other questions tagged

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