大家好我是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中通过
'返回$此模型;'检查这个地方 – sectus
是它已经完成。 –
我想知道你是否必须从'Car :: __ construct()'中运行'parent :: __ construct()'来获得在你的'Car'中设置的属性? –