Nie działa auto ładowanie klas gdy korzystam z przestrzeni nazw

0

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.
0

Nie, to jest inaczej:

plik index.php

<?php
    spl_autoload_extensions(".php");
    spl_autoload_register();

    use Cars\Car;
    use Cars\Car2;

    $car = new Car();
    $car->setCounter(500);
    $car->setCountry('PL');
    $car->setYear(2009);
    $car->setOwner('MW');

    $car2 = new Car2();
    $car2->setCounter(500);
    $car2->setCountry('PL');
    $car2->setYear(2009);
    $car2->setOwner("MW");
?>

<!DOCTYPE html>
<html lang="pl">
<head>
    <meta charset="UTF-8">
    <title>Nauka php</title>
</head>
<body>

<pre>
    <?= print_r($car) ?>
    <?= print_r($car2) ?>
</pre>

</body>
</html>

pliki Car.php oraz Car2.php umieszczone w podkatalogu Cars, index.php w katalogu głównym projektu.

Cars/Car.php:

<?php
//tutaj namespace związane z nazwą podkatalogu 
namespace Cars;

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;
    }
}

Dla Cars/Car2 ma być podobnie (to samo namespace).

I jeszcze to:

Good news for PHP 5.3 users with namespaced classes:

When you create a subfolder structure matching the namespaces of the containing classes, you will never even have to define an autoloader.

    spl_autoload_extensions(".php"); // comma-separated list
    spl_autoload_register();

źródło:
http://php.net/manual/en/function.spl-autoload-register.php

Poza tym jeśli masz wstawiać wartości tak

    $car = new Car();
    $car->setCounter(500)->setCountry('PL')->setYear(2009)->setOwner('MW');

To każdy setter w Twojej klasie musi zwracać $this.

    public function setYear($year)
    {
        $this->year = $year;
        return $this;
    }

I tak w każdym seterze. Właściwie to w ogóle nie musisz nawet używać tych pól prywatnych i geterów, seterów, bo możesz równie dobrze użyć stdClass i tam tylko prosto przypisywać:

    $car = new stdClass();
    $car->counter = 500;
    $car->country = 'PL';
    $car->year = 2009;
    $car->owner = 'MW';

1 użytkowników online, w tym zalogowanych: 0, gości: 1