To work this type of code and automatically load by namespace directories must follow the same name as in namespace, in your case already have a problem the directory should be minhaClasse/classe.php
or else the namespace should be use classes\classe;
(within the class only namespace classes;
) any of the options may work.
Edit the namespace of his classe
for classes
and to use use classes\classe
:
namespace classes;
class classe
{
public function testes(){
return "ok.. retornou!";
}
}
and in your code, make changes to namespace, that will work:
spl_autoload_extensions(".php");
spl_autoload_register();
use classes\classe;
$ver = new classe();
echo $ver->testes();
Observing: the names of namespace
and pastas
(that must have the same name) could follow names as simple as possible and without reference to names that can make reading difficult, this can cause confusion classes\classe
Minimal example:
The code of the class Car
is as follows (observe the namespace
and the name of diretório
in the image):
<?php
namespace Novic;
class Car
{
private $active = 1;
public function getActive()
{
return $this->active;
}
public function setActive($value)
{
return $this->active = $value;
}
}
inside the previous folder (i.e., in the folder Test1) a index.php
with the following code:
<?php
spl_autoload_extensions(".php");
spl_autoload_register();
use Novic\Car;
$car = new Car();
echo $car->getActive();
The namespace
used Novic\Car
referring to folder and class.
Reading:
You have it here on the website How namespaces work in PHP? and also on PSR-4, that is well used.
References:
It worked Virgil. Thank you very much for the clarifications and indication of reading.
– user24136