我有一个名为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";
?>
父和desc字段在这里混淆了,我找不到原因。请帮忙!
答案 0 :(得分:5)
不应该这个
public function setDesc($desc){
$this->parent = $desc;
}
有
$this->desc = $desc;
代替?