这是我的三页。
OOexamples.php:
function __autoload($class_name) {
require_once 'includes/'.$class_name . '.php';
}
$ObjColl = new ObjectCollection();
$bike1 = new Bike("R1", 1000);
$ObjColl->addItem($bike1);
$bike2 = new Bike("R6", 600);
$ObjColl->addItem($bike2);
$bike3 = new Bike("Bandit", 1000);
$ObjColl->addItem($bike3);
$bike4 = new Bike("Trial", 600);
$ObjColl->addItem($bike4);
for($i = 0;$ObjColl->getItemCount();$i++){
$item = $ObjColl->getItem($i);
if ($item instanceof Bike) {
print "Bike - ";
}
print $item;
}
ObjectCollection.php:
class ObjectCollection
{
//This is an array to hold line items
private $items_array ;
private $itemCounter; //Count the number of items
public function __construct() {
//Create an array object to hold line items
$this->items_array = array();
$this->itemCounter=0;
}
public function getItemCount(){
return $this->itemCounter;
}
public function addItem($item) {
$this->itemCounter++;
$this->items_array[] = $item;
}
public function getItem() {
return $this-> Bike = $item;
}
}
我80%确定它与该功能有关" getItem"以上。该行"返回$ this-> Bike = $ item;"使我的页面变白,它永远不会停止加载?
Bike.php:
class Bike {
private $name;
private $model;
private $cc;
public function __construct($name,$model){
$this->name = $name;
$this->model = $model;
}
public function set_cc($cc) {
$this->cc = $cc;
}
public function get_cc() {
return $this->cc;
}
}
为什么我的每个循环都没有打印出Bike对象?我知道它与OOexamples页面上的每个循环有关。请帮助它让我发疯。我是OO的初学者,所以很容易。我得到的错误是"致命错误:无法访问第27行"中包含/ ObjectCollection.php中的空属性;
答案 0 :(得分:1)
for($i = 0;$ObjColl->getItemCount();$i++){
应该是:
for($i = 0;$i < $ObjColl->getItemCount();$i++){
否则你的循环永远不会退出,因为没有比较。
还有一个错误:
public function getItem() {
return $this-> Bike = $item;
}
不确定这会做什么,但$this->Bike
不存在,因为类ObjectCollection
没有属性Bike
。因此,假设您希望方法getItem按给定索引返回项目,它应该是:
public function getItem($index) {
return $this->items_array[$index];
}
总之,这是您的计划的外观:
<强> OOExamples.php:强>
function __autoload($class_name) {
require_once 'includes/'.$class_name . '.php';
}
$ObjColl = new ObjectCollection();
$bike1 = new Bike("R1", 1000);
$ObjColl->addItem($bike1);
$bike2 = new Bike("R6", 600);
$ObjColl->addItem($bike2);
$bike3 = new Bike("Bandit", 1000);
$ObjColl->addItem($bike3);
$bike4 = new Bike("Trial", 600);
$ObjColl->addItem($bike4);
for($i = 0;$i < ObjColl->getItemCount();$i++){
$item = $ObjColl->getItem($i);
if ($item instanceof Bike) {
print "Bike - ";
}
print $item->getName();
}
<强> ObjectCollection.php:强>
class ObjectCollection
{
//This is an array to hold line items
private $items_array ;
private $itemCounter; //Count the number of items
public function __construct() {
//Create an array object to hold line items
$this->items_array = array();
$this->itemCounter=0;
}
public function getItemCount(){
return $this->itemCounter;
}
public function addItem($item) {
$this->itemCounter++;
$this->items_array[] = $item;
}
public function getItem($index) {
return $this->items_array[$index];
}
}
<强> Bike.php:强>
class Bike {
private $name;
private $model;
private $cc;
public function __construct($name,$model){
$this->name = $name;
$this->model = $model;
}
public function setCC($cc) {
$this->cc = $cc;
}
public function getCC() {
return $this->cc;
}
public function setName($name) {
$this->name = name;
}
public function getName() {
return $this->name;
}
public function setModel($model) {
$this->model = model;
}
public function getModel() {
return $this->model;
}
}