neknek mouh
neknek mouh

Reputation: 1802

PHP access methods from parent class

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

Upvotes: 0

Views: 142

Answers (2)

Tristan
Tristan

Reputation: 2399

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();

Upvotes: 0

zongweil
zongweil

Reputation: 2051

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.

Upvotes: 1

Related Questions