0

Hello guys I'm new to PHP's OOP so I need a little help from my test scripts. This is what I've tried so far:

index.php

    <?php 
    include("shared.php"); 
?>

<!DOCTYPE html>
<html>
<head>
<title>Car Details</title>
</head>

<body>
<?php 

    $car1 = new Car("Audi");
    echo $car1->showCarDetails();

?>
</body>
</html>

car.php

    <?php 

class Car extends CarDetails {

    public $name;
    public $color = "Freaking Sexy White";
    public $price = "PHP 4,000,000.00";

    public function _construct($name) {

        $this->setName($name);
        $this->getColor();
        $this->getPrice();

    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setColor($color) {
        $this->color = $color;
    }

    public function setPrice($price) {
        $this->price = $price;
    }

    public function getName() {
        return $this->name;
    }

    public function getColor() {
        return $this->color;
    }

    public function getPrice() {
        return $this->price;
    }

    public function showCarDetails() {
        print nl2br("I have an awesome car. Below are the details :)\r\n".
                "Brand: " . $this->getName() . "\r\n" . 
                "Model: " . parent::getModel(). "\r\n" .
                "Color: " . $this->getColor() . "\r\n" . 
                "Price: " . $this->getPrice()
                );


    }

}


?>

cardetails.php

    <?php 

class CarDetails {

    public $model = "A7 Sportback";
    public $engine = "FSI technology";

    public function setModel($model) {
        $this->model = $model;
    }

    public function getModel() {
        return $this->model;
    }

    public function setEngine($engine) {
        $this->engine;
    }

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

}

?>

shared.php

    <?php 

function __autoload($className)
{
    //echo "We are requesting the " . $className . " class";

     if(file_exists($className . ".php"))
    {
        require_once($className . ".php");
        //echo "The " . $className . " has been included";
    }

}

?>

I want to access the method from my parent class which is CarDetails.php, getModel() and getEngine(). But I don't know how to do that, and also what I have declared in the constructor of Car.php in my index.php is not found.

The output:

Notice: Object of class Car could not be converted to int in C:\xampp\htdocs\oop\cardetails.php on line 13
I have an awesome car. Below are the details :)
Brand:
Model: 1
Color: Freaking Sexy White
Price: PHP 4,000,000.00 

But my intended output should be:

I have an awesome car. Below are the details :)
Brand: Audi
Model: A7 Sportback
Color: Freaking Sexy White
Price: PHP 4,000,000.00 

What is the problem in my code? Any ideas? I'd truly appreciate your help. Thanks.

UPDATE:

I can now access the methods from my parent class. But the problem is, I'm not seeing anything that I declared in my constructor.

Brand: __

Where it should be:

Brand: Audi

Since I passed in "Audi" in index.php

7
  • return $this-model; check this place Commented Mar 14, 2014 at 4:56
  • Yes it's already done. Commented Mar 14, 2014 at 4:56
  • I wonder if you have to run parent::__construct() from Car::__construct()to get those properties set in your Car? Commented Mar 14, 2014 at 4:57
  • You extended the class and the parent class has those methods as public, so you can access them as if they were in the child class. Commented Mar 14, 2014 at 4:58
  • 1
    Car::__construct is missing an underscore too. It's only got one. Commented Mar 14, 2014 at 5:00

2 Answers 2

1

You have a couple of typos in cardetails.php:

public function setEngine($engine) {
    $this->engine;
}

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

should instead be

public function setEngine($engine) {
    $this->engine = $engine;
}

public function getEngine() {
    return $this->engine;
}

Also, in car.php:

public function _construct($name) {

should be

public function __construct($name) {

I believe that's causing the weirdness you're seeing.

Sign up to request clarification or add additional context in comments.

Comments

0

So I've made a few changes here, the whole point behind extending your class is so that your child class HAS the public/protected functionality that the parent class had.

This would mean that your child class Car shouldn't need to call parents when accessing the getModel or any other functions.

You can see the code changes run live here, https://ideone.com/vCfxYQ

<?php
class Car extends CarDetails {

    public $name;
    public $color = "Freaking Sexy White";
    public $price = "PHP 4,000,000.00";

    public function __construct($name) {

        $this->setName($name);
        $this->getColor();
        $this->getPrice();

    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setColor($color) {
        $this->color = $color;
    }

    public function setPrice($price) {
        $this->price = $price;
    }

    public function getName() {
        return $this->name;
    }

    public function getColor() {
        return $this->color;
    }

    public function getPrice() {
        return $this->price;
    }

    public function showCarDetails() {
        print nl2br("I have an awesome car. Below are the details :)\r\n".
                "Brand: " . $this->getName() . "\r\n" . 
                "Model: " . $this->getModel(). "\r\n" .
                "Color: " . $this->getColor() . "\r\n" . 
                "Price: " . $this->getPrice()
                );


    }

}

class CarDetails {

    public $model = "A7 Sportback";
    public $engine = "FSI technology";

    public function __construct() {

    }

    public function setModel($model) {
        $this->model = $model;
    }

    public function getModel() {
        return $this->model;
    }

    public function setEngine($engine) {
        $this->engine = $engine;
    }

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

}

$car1 = new Car("Audi");
echo $car1->showCarDetails();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.