Using $this, self::, parent:: for code readability

前端 未结 6 723
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 13:51

I would like to know if it is acceptable/preferred to use self::method() and parent::method() when working in php classes.

You can use $this->method() but $this-> can al

6条回答
  •  难免孤独
    2021-02-08 13:58

    another thing to note by the way is that this isn't very good MVC design to be making static controller functions that return lists of categories.

    Controllers are for dealing with requests, Models are for dealing with data (which is what this is) and Views are for display.

    make a model!

    class Category_Model extends Model
    {
          public function categories($site_id)
          {
                $categories = $this->db->from('forum_categories')->where('fk_site',$site_id)->get();
    
                    return new View('categories_view', array('categories' => $categories)); 
          }
    }
    

    ...

    $cat = new Category_Model;
    
    echo $cat->categories(1);
    

提交回复
热议问题