2013-06-03 2 views
0

필드와 메소드가있는 클래스라는 클래스가 있습니다. 배열 변수가 어딘지에서 섞여 있고 이것을 알아 내려고 한 시간을 보냈습니다. 여기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"; 
    ?> 

부모와 내림차순 필드가 여기에 혼합지고 코드이며, 그 이유를 찾을 수 없습니다. 도와주세요!

+0

당신은 왜'$ nc = new Category; '를 가지고 있습니까? 대신 그 줄을 제거하고'$ nc->'대신'$ this->'를 사용하십시오. 그리고'$ catObject = new Category();'는'$ catObject = new Category; '이어야합니다. – Richard

답변

5

public function setDesc($desc){ 
     $this->parent = $desc; 
    } 

대신

$this->desc = $desc; 

은하지 말았어야?

+0

oops! 고맙습니다! 죄송합니다 그때 =/ – digitalseraph

+0

답변을 수락 한 것으로 표시하십시오. (당신은 녹색으로 표시된 부호를 볼 것입니다.) –

+0

메모로서, 나는 또한 desc가 PHP가 아닌 – digitalseraph