我有一个名为 Category 的类,它有字段和方法,数组变量不知何故混淆了,我花了一个小时试图弄清楚这一点。这是代码
<?php
/**
* This is a Category class
*
* @author Seraph
*/
class Category {
private $id;
private $parent;
private $title;
private $desc;
protected $newCategories = array();
public function _construct() {
$this -> id = NULL;
$this -> parent = NULL;
$this -> title = NULL;
$this -> desc = NULL;
}
/*
* Function to add a new category to the newCategories array
*/
public function addNewCategory($parent,$title,$desc) {
$nc = new Category;
$nc->setId(0);
$nc->setParent($parent);
$nc->setTitle($title);
$nc->setDesc($desc);
$this->newCategories[$title] = $nc;
}
/*
* Function to remove a category from the newCategories array
*/
public function removeNewCategory($title) {
$tempArray = $this->newCategories;
for($i=0;$i<count($tempArray,1);$i++) {
$element = $tempArray[$title];
if($tempArray[$title] == $title) {
$tempArray = array_diff($tempArray, array($title));
}
}
$this->newCategories = $tempArray;
}
/*
* Getters and Setters
*/
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getParent() {
return $this->parent;
}
public function setParent($parent){
$this->parent = $parent;
}
public function getTitle() {
return $this->title;
}
public function setTitle($title){
$this->title = $title;
}
public function getDesc() {
return $this->desc;
}
public function setDesc($desc){
$this->parent = $desc;
}
public function getNewCategories() {
return $this->newCategories;
}
}
//TESTER
$catObject = new Category();
$catObject->addNewCategory("0", "City Scape", "This is a description of the category.");
$nc = $catObject->getNewCategories();
var_dump($nc["City Scape"]);
echo $nc["City Scape"]->getParent() . "\n";
echo $nc["City Scape"]->getTitle() . "\n";
echo $nc["City Scape"]->getDesc() . "\n";
?>
parent 和 desc 字段在这里混淆了,我不知道为什么。请帮忙!