Jak w temacie mam problem z auto ładowaniem klas gdy zacznę korzystać z przestrzenie nazw.
Gdy nie korzystam z przestrzeni nazw wszystko działa.
Struktura katalogów ma się tak.
W głównym plik index.php o raz folder app gdzie mam plik autoload ładujący klasy z znajdującego się w tym folderze folderu class.
Pliki:
index.php
<?php
use Car\Car;
use Car2\Car2;
include ('app/autoload.php');
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Nauka php</title>
</head>
<body>
<?php
$car = new Car();
$car->setCounter(500);
$car->setCountry('PL');
$car->setYear(2009);
$car->setOwner('MW');
?>
<pre>
<?=print_r($car);?>
</pre>
<?php
$car2 = new Car2();
$car2->setCounter(500)->setCountry('PL')->setYear(2009)->setOwner("MW");
?>
<pre>
<?=print_r($car2);?>
</pre>
</body>
</html>
autoload.php
<?php
function autoload($class)
{
$path = __DIR__.DIRECTORY_SEPARATOR.'class'.DIRECTORY_SEPARATOR. $class . '.php';
include ($path);
echo 'Próbowano załadować: '.$path;
}
spl_autoload_register('autoload');
Klasy Car1 oraz Car2
<?php namespace Car; ```php class Car { private $year; private $country; private $owner; private $counter; public function setYear($year) { $this->year = $year; } public function setCountry($country) { $this->country = $country; } public function setOwner($owner) { $this->owner = $owner; } public function setCounter($counter) { $this->counter = $counter; } } ``` ```php <?php namespace Car2; class Car2 { private $year; private $country; private $owner; private $counter; public function setYear ($year) { $this->year = $year; return $this; } public function setCountry ($country) { $this->country = $country; return $this; } public function setOwner ($owner) { $this->owner = $owner; return $this; } public function setCounter ($counter) { $this->counter = $counter; return $this; } } ``` Ścieżka path zwracana niby poprawnie a jednak jak włączę przestrzenie nazw to nie działa.