필드와 메소드가있는 클래스라는 클래스가 있습니다. 배열 변수가 어딘지에서 섞여 있고 이것을 알아 내려고 한 시간을 보냈습니다. 여기PHP 배열 변수 혼합하기
<?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";
?>
부모와 내림차순 필드가 여기에 혼합지고 코드이며, 그 이유를 찾을 수 없습니다. 도와주세요!
당신은 왜'$ nc = new Category; '를 가지고 있습니까? 대신 그 줄을 제거하고'$ nc->'대신'$ this->'를 사용하십시오. 그리고'$ catObject = new Category();'는'$ catObject = new Category; '이어야합니다. – Richard