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();?>