So carrying on my earlier post about classes and cars.

https://board.phpbuilder.com/d/10402542-oop-list-of-objects/7

I have a new question.

I have a Car class. I have a CarsList class (listing all the cars in my database) but I also want a Fleet class.

This will be a list of Car objects but with more information such as FleetID, Delivered, ArrivalDate, CurrentValue etc.

Should I just add more fields to the Car class and leave them blank when not used or should I have a FleetCar class which is exactly the same as Car class but with more information?

    Well, if a FleetCar is a kind of Car (in that everything that applies to a Car applies to a FleetCar as well, but the FleetCar has extra stuff) then making FleetCar a subclass of Car would make sense.

      Interesting, So like my Car and CarList classes, I should have FleetCar and FleetCarList classes.

      I create FleetCar with specific information and about and extend it to Car class.

      When I create FleetCar I do this

      $FleetCar = new FleetCar('Data1', 'Data2');

      But I also have make, model cost etc from my Car class. How do I set these values to my FleetCar object?

        So if Car class has just make and model in its constructor

        Public Class Car {
        public __construct($make, $model){
        $this->make = $make;
        $this->model = $model;
        }
        }

        And then Fleet Car class has age, milage for example

        Public Class FleetCar extends Car {
        public __construct($age, $mileage){
        parent::__construct();
        $this->make = $age;
        $this->model = $milage;
        }
        }

        How when I create a fleet car can I pass in Car variables?

        $myCar = FleetCar(15, 1234132);

        I need to pass in make and model too don't I?

          class Car
          {
            public function __construct($make, $model)
            {
              $this->make = $make;
              $this->model = $model;
            }
          }
          
          
          class FleetCar extends Car
          {
            public function __construct($make, $model, $age, $mileage)
            {
              parent::__construct($make, $model);
              $this->make = $age;
              $this->model = $mileage;
            }
          }
          

          However, I find myself sort of leaning toward composition instead of inheritance again, though it's not a clear-cut thing to me -- might depend on a better understanding of the underlying business logic. If I did go that way, then similar to the Database example, I might pass an instance of a Car into the FleetCar constructor -- but I'm by no means sure I'd take that path right now. šŸ™‚

          So I’m replying from my phone so this make not work well but

          how do I create this object though

          $fc = new FleetCar(12,123123);

          how do I set car properties into my fc object?

            NogDog showed how to modify the FleetCar constructor signature to accept four params, two of which were used to construct the parent object.

            If you can't modify the __construct() declaration, you'll have to find some other way to skin that cat; none of which feel like they'd be nearly as clean as what NogDog has posted above. Keep in mind that function calls can have optional params also ($foo = '', null, etc.) or could accept an array or even an object as a constructor argument.

              Oh I missed some of NogDog's replace on my phone.

              So does the parent class need to be added first when creating the object? what's the logic /rules around this?

              NZ_Kiwis So does the parent class need to be added first when creating the object? what's the logic /rules around this?

              The child class definition needs to know its parent's definition (e.g. you may need to require() the parent's class file in the child's file); but when instantiating the child class, you do not have to do anything w.r.t. the parent class.

                Write a Reply...