Error Uncaught Error: Class 'Client Register' not found Using namespace

Asked

Viewed 670 times

0

I am studying about namespaces, I wrote the code below, but is returning the following error:

string(16) "Client Register" 'Client Register' not found in /var/www/html/curso_php/namespace/index.php:13 Stack trace: #0 {main} thrown in /var/www/html/curso_php/namespace/index.php on line 13

In the config.php file var_dump() should return the two classes.

Folder structure:
|-Project
|-config.php
|-index php.
|--class
|---Cadastro.php
|---Client
|-----Cadastro.php

config.php

<?php
    spl_autoload_register(function($nameClass){

        var_dump($nameClass);

        $dirClass = "class";
        $filename = $dirClass . DIRECTORY_SEPARATOR . $nameClass . ".php";

        if (file_exists($filename) === true) {
            require_once $filename;
        }

    });?>

Client Cadastro.php

<?php
namespace Cliente {

    class Cadastro extends \Cadastro {
        public function registrarVenda() {

            echo "foi registrada uma venda para o cliente " . $this->getNome();

        }
    }
}?>

Cadastro.php

    public function getEmail():string
    {
        return $this->email;
    }


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


    public function getSenha():string
    {
        return $this->senha;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function setNome($nome)
    {
        $this->nome = $nome;
    }

    public function setSenha($senha)
    {
        $this->senha = $senha;
    }

    public function __toString()
    {
        return json_encode(array(
            "nome"=>$this->getNome(),
            "email"=>$this->getEmail(),
            "senha"=>$this->getSenha()
        ));
    }
}?>

index php.

<?php
require_once "config.php";

use Cliente\Cadastro;

$cad = new Cadastro();
$cad->setNome("Matheus");
$cad->setEmail("[email protected]");
$cad->setSenha("12345");

echo $cad;
echo "--------------<br>";

$cad->registrarVenda();?>

2 answers

0

Check your Register.php class, the error I think is in it:

class Cadastro {
    private $nome;
    private $email;
    private $senha;

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

    public function getEmail():string{
        return $this->email;
    }

    public function getSenha():string{
        return $this->senha;
    }

    public function setNome($nome){
        $this->nome = $nome;
    }

    public function setEmail($email){
        $this->email = $email;
    }

    public function setSenha($senha){
        $this->senha = $senha;
    }

    public function __toString(){

        return json_encode(
            array(
                "nome"=>$this->getNome(),
                "email"=>$this->getEmail(),
                "senha"=>$this->getSenha()
            )

        );

    }

}

-1

In the config file, exchange:

$filename = $dirClass . DIRECTORY_SEPARATOR . $nameClass . ".php";

for:

$filename = str_replace ("\\", "/", $dirClass . DIRECTORY_SEPARATOR . $nameClass . ".php");

Browser other questions tagged

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