2013-06-03 94 views
0

我有一個名爲Category的類,它有字段和方法,數組變量混合了一些,我花了一個小時試圖解決這個問題。這裏是代碼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"; 
    ?> 

父和desc字段越來越混淆在這裏,我找不到原因。請幫忙!

+0

爲什麼你有'$ nc = new Category;'?而不是刪除該行,並使用'$ this->'而不是'$ nc->'。 而'$ 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