Anyone able to help me with PHP oriented objects?

This is the question at hand, and I have no idea where to start.

Purpose: This is an exercice to test your basic knowledge of interface and class in PHP oriented object.

Instruction:

  1. Create an interface class called Car. It should contain only a single method, finalCost();
  2. Create another class, Chevy, that implements Car. It should have a protected variable called cost. It should have a constructor which takes the cost in argument and sets it. It should have the method finalCost() which takes in argument a tax percentage and returns the final cost of the car in $ using the cost set when the object is initialized.
  3. Use the Chevy class to get the cost of a Chevy with a tax of 12% and the initial cost of 20000. Echo the cost in the screen.
  4. Please put all your code below, do not create a separate file. Do the runtime execution below as well. You can wrap it in HTML doc structure for bonus points.
  5. Bonus: if you have time, create a class called Ford which implements Car and have the cost calculated. However this time, Ford has a handling fee attached to it of $1,000 to be calculated before tax.

Well, this site is to help you once you show us some code. We could TEACH you, but, that is why you have books and online courses to learn. But, here is a simple tutorial on how to create an OOP Class in simple terms. You can change the fruit to cars and sort it out… When you get stuck, post some code here and someone can help you fix it. OOP Classes With Objects
Hope this gets you started… Good luck!

Thank you! I believe I was on a similar resource. Is what I have a step in the right direction?

<?php 

interface Car {
    public function finalCost();
}

class Chevy implements Car {

    protected $cost = [];

    function __construct($cost) {
    
    }

}

?>

Yes, you are close. Here is another page from the previous link.
It shows a class with cats and dogs. You can change them for chevys and fords… Should work…
https://www.w3schools.com/php/php_oop_interfaces.asp

Does that link say anything about constructors though? I’m not sure if I messed up anything with my code already since I don’t know how to make a constructor work. But I understand your link is referring to abstract classes which I need too.

Yes, on the left side just select constructors and it explains those, too.
I thought I had said that you can select an item on the left in the group you are viewing and
learn about various parts of the group. In this case oop-classes. I don’t use these often,
just have not needed them, but, others here have and will help if you post code.

Okay thank you for your help!

Here’s what I have so far. Where can I input the cost of 20000, and tax of 12%? And do let me know if I have any mistakes so far.

<?php 

interface Car {
    public function finalCost();
}

class Chevy implements Car {

    protected $cost = [];

    function __construct($cost) {
        $this->cost = $cost;
    } 

    function finalCost() {
        return $cost->cost;

    }

}

?>

So, let’s review…

You create an interface for Car
-----> Then, you have a class of the interface for Chey
-----> ----> And, you create functions for use of the Chevy…

All good so far…

In your functions, you construct the $cost of it, meaning the class Chevy.
In the functions finalCost, you should return the cost plus the 12%. Therefore in that function you need to calculate final cost of the car. Meaning something like:

<?php 

interface Car {
    public function finalCost();
}

class Chevy implements Car {
    protected $cost = [];

    function __construct($cost) {
        $this->cost = $cost;
    } 

    public function finalCost() {
        return $this->cost + $this->cost*.12;
    }
}

$mycar = new Chevy(20000);
echo $mycar->finalCost();

?>

This depends on how your teacher likes to display values.

And, you would need to call the interface/class/function with the original value of 2000. Hope this helps!

1 Like

You would have to duplicate the chevy’s class for the Ford version…

Ohh I got it now. I was really close near the end there. I just had no idea what to return, but I had the rest of it in after some revision. Thank you so much for helping me figure it out!

No problem at all. Just don’t like to do someone’s homework… Ha! That is why I made you work at it a bit. Hope you understood it all.

1 Like

I hear you haha, I would be doing much more research but I’d been reading the same things so many times without any explanation. I do understand it now though.

OP, there is no reason to set $cost to an empty array. There are no arrays at play here.

You can also chain your method.

echo (new Chevy(20000))->finalCost();

If you were doing Php8 there are more “improvements” that could be done.

@hollowwizard do you understand what an interface is, what it’s used for, and why it’s important?

As per instructions, tax needs to be an argument of finalCost method.

Sponsor our Newsletter | Privacy Policy | Terms of Service