2014-03-14 65 views
0

大家好我是PHP的OOP的新手,所以我需要我的测试脚本提供一些帮助。 这是我到目前为止已经试过:父类的PHP访问方法

的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()。但我不知道该怎么做,也没有找到我在Car.php在我的index.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 

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

UPDATE:

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

品牌:_ _

,它应该是:

品牌:奥迪

自从我在 “奥迪” 的index.php中通过

+0

'返回$此模型;'检查这个地方 – sectus

+0

是它已经完成。 –

+0

我想知道你是否必须从'Car :: __ construct()'中运行'parent :: __ construct()'来获得在你的'Car'中设置的属性? –

回答

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) { 

我相信这是导致你所看到的怪事。

+0

是的,这是感谢你的朋友! –

+0

如果这解决了您的问题,请将答案标记为已接受:) – zongweil

0

所以我在这里做了一些修改,扩展类的后面的要点是让你的子类具有父类所具有的公共/保护功能。

这意味着您的孩子班车在访问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();