2
\$\begingroup\$

I'm struggling with organizing my code regarding 'best practise'. In particular how I link a class with its collection class.

I've 2 classes:

class car
{
    private $color;
    private $model;
    private $speed;

    public function getColor() {}
    public function setColor() {}
    public function getModel() {}
    public function setModel() {}
    public function getSpeed() {}
    public function setSpeed() {}
}

class carLoader
{
    public function loadAllCars()
    {
        // load all cars from the database
        ...

        // iterate through the result and create an object for each car
        $allCars = array();
        foreach ($result as $carName => $carData) {
            $allCars[$carName] = $this->createCar($carData);
        }

        return $allCars;
    }


    private function createCar(array $carData)
    {
        $car = new Car();

        $car->setColor($carData['color']);
        $car->setModel($carData['model']);
        $car->setSpeed($carData['speed']);

        return $car;
    }
}

Now I have an array with all car objects. So far so good. However, I am not sure whats the best approach to save manipulated properties in the database. Or in other words, how to connect the property class 'car' to the database because (as far as I've learned) it should NOT contain any database queries but only the properties.

One solution I came up with is to add a collection function which holds an object of a new 'collection' class. So my solution would look like:

class car
{
    private $color;
    private $model;
    private $speed;

    private $collection;

    public function getColor() { return $this->color; }
    public function setColor() { return $this->getCollection()->saveColor($value); }
    public function getModel() { return $this->model; }
    public function setModel() { return $this->getCollection()->saveModel($value); }
    public function getSpeed() { return $this->speed; }
    public function setSpeed() { return $this->getCollection()->saveSpeed($value); }

    public function getCollection()
    {
        return $this->collection;
    }

    public function setCollection($collection) 
    {
        $this->collection = $collection
}

class carLoader
{

    public function __construct(DbManager $pdo)
    {
        $this->pdo = $pdo;
    }

    public function loadAllCars()
    {
    // load all cars from the database
    ...

    // iterate through the result and create an object for each car
    $allCars = array();
    foreach ($result as $carName => $carData) {
        $allCars[$carName] = $this->createCar($carData);
    }

    return $allCars;
    }

    private function createCar(array $carData)
    {
        $car = new Car();

        $car->setColor($carData['color']);
        $car->setModel($carData['model']);
        $car->setSpeed($carData['speed']);
        $car->setCollection($this-getCollection());

        return $car;
    }

    private function getCollection()
    {
        if ($this->carCollection === null) {

            $this->carCollection = new CarCollection($this->pdo);
        }

        return $this->carCollection;
    }
}

class carCollection()
{
    public function __construct(DbManager $pdo)
    {
        $this->pdo = $pdo;
    }

    public function getCollection()
    {
        // logic to save the data in the database
    }
}

Is this a 'good' way to do it if I do need to work with the array of car objects? How else should I save new values (color, speed, model) in the database?

Thank you very much

\$\endgroup\$
  • 1
    \$\begingroup\$ I have no idea about 'best practises', but what I would do is create another class containing the methods to get and set properties in the database: public function getProperty($name) and public function setProperty($name,$value). Let's call it 'base', for now. The methods in this base class can then be inherited by the car class: class car extends base, so car can use these methods. That way you can still swap out the underlying base class by another when you get another database system, without having to alter the car class. \$\endgroup\$ – KIKO Software Sep 24 '18 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.