父类的PHP访问方法

时间:2014-03-14 04:53:20

标签: php

大家好,我是PHP的OOP的新手,所以我需要一些测试脚本的帮助。 这是我到目前为止所尝试的:

的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";
    }

}

?>

我想从我的父类访问该方法,该方法是 CarDetails.php getModel() getEngine()。但我不知道该怎么做,而且我在 index.php 中的 Car.php 的构造函数中声明的内容也找不到。

输出:

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 

但我的预期输出应该是:

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

我的代码有什么问题?有任何想法吗?我真的很感谢你的帮助。感谢。

更新:

我现在可以从父类访问方法了。但问题是,我没有看到我在构造函数中声明的任何内容。

品牌: _ _

它应该在哪里:

品牌:奥迪

因为我在index.php中传递了“Audi”

2 个答案:

答案 0 :(得分:1)

你在cardetails.php中有一些拼写错误:

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

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

应该是

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

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

另外,在car.php中:

public function _construct($name) {

应该是

public function __construct($name) {

我相信这会导致你所看到的怪异。

答案 1 :(得分:0)

所以我在这里做了一些更改,扩展你的课程背后的重点是让你的子课有父类的公共/受保护功能。

这意味着您的孩子类Car在访问getModel或任何其他功能时不应该呼叫父母。

您可以在此处查看代码更改,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();
相关问题