Welcome Guest, Not a member yet? Register   Sign In
Applying Inheritance
#1

[eluser]Unknown[/eluser]
Dear fellow CI developer,
I'm in the process of developing a CMS (content management system) application. and typically CMS will have "users" that can login to edit the content of their article/post/message. I'm adding an "admin" control which also need to login, but they will be able to edit/approve user's article.

so my practice on this is to create 2 tables "admin" and "user" and two model "admin_model.php" and "user_model.php".
in the process of doing this I started to realize that they both share very similar behaviour. So I was thinking to apply the inheritance approach for this.
Since user is more basic and broad, I put user as the parent and admin as the child.

here is my two model code.
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends model
{  
    var $table_name;
        // -- Constructor -----------------------------------------------  
    public function __construct()  
    {    
        parent::Model();
        $this->table_name = "user";
    }

    public function get_user($id)  
    {        
        $query = $this->db->query("SELECT * FROM $this->table_name
                                           WHERE id = '$id'");
        $row = $query->row();
        return $row;
    }
}
?>

Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once('user_model.php');

class admin_model extends User_model
{
    var $table_name;
    // -- Constructor -----------------------------------------------  
    public function __construct()  
    {    
        parent::__construct();
        $this->table_name = "admin";
    }
}

and in my controller I just load them by using "$this->load->model('Admin_model');"
I just wondering if my structure, the way I extends the parent, the way I assign my $table_name value is correct? should I declare the table_name value inside the constructor or outside? I still have to call the parent's constructor right? eventhough I don't want to use its "$table_name".
Also I don't have to write my function get_user($id) in my admin_model right? since I can just use the parent's function?

Thank you =)
#2

[eluser]chobo[/eluser]
I'm not too great with OOP and design patterns, but I suggest that you make an abstract parent class (CMS_User) with properties and methods common to both child classes. Why? Just because user is an actual implementation, so it makes sense to me to do it that way. Like I said I'm not very good with this OOP (I try Smile), so you might wanna get a few more opinions on the issue.
#3

[eluser]Unknown[/eluser]
Awesome, chobo yes you right. I'm declaring my user_model class as abstract class now.
this is the right way to implement it since I'm planning to never instatiate user_model directly anyway.
Another question pop up though:
when declaring an abstract class, functions has to be declared as abstract too right? is it a good practice to leave some function abstract and some not in an abstract class?
because I got an error abstract function cannot contain a body when I have something inside that function.
The reason is that I still want to use some common function in the user_model like get_user($id), get_all_user(), etc. these function is going to be the same for every class that extends the abstract user_model class.

Thanksss
#4

[eluser]chobo[/eluser]
In an abstract class you can have a mix of abstract methods and non-abstract methods; I think this goes for properties as well. An interface is something that can only contain abstract methods and properties, but an abstract class can have a mix. If you have child classes that can build off an implementation of a base class (parent class) then you can override the method in the child classes and add to it, by calling the original.

Ex


Code:
// Parent class

protected function testOutput()
{
   $test = "This is from the parent class, and pretty much every child class uses this text";
   return $test;
}


Code:
// Child Class

//override function from parent
protected function testOutput()
{
  $test = parent::testOutput();
  $test .= $test . "Some other text specific to this child class";
  return $test;
}


Sometimes when you put implementations in parent classes down the road they change. It's hard to design a "perfect" system and it's best to just experiment with different techniques, and you'll pick it up. I'm still in that stage as well Smile

* When you declare a function as abstract it has to be implemented in the inheriting classes and will give you an error if you try and define an implementation where you declared the function as abstract.




Theme © iAndrew 2016 - Forum software by © MyBB