Welcome Guest, Not a member yet? Register   Sign In
use model or library?
#1

[eluser]Unknown[/eluser]
Hi all, im making an application that documents a datacenter. A bit of background info. The database design models every physical object in the datacenter (rack, server, room, etc) as a db object with a parent and multiple children. Each object is of a specific type. In PHP, the idea is to have classes represent each type. Something like:

/* super class */
class Containers {
....
}
class Rack extends Containers {

function render() { .. }
function create() { .. }
..
}
class Switch extends Containers {
function render() { .. }
function create() { .. }
..
}

When the application needs to show a certain item, it knows the type, and create a new object of that type.

$object = new $type;

It can then perform functions specific to that object, like render it on screen, create a new one, delete it, modify it, etc. Each class will have its own set of similar functions as each type needs to be rendered, created, etc differently. This is of course a heavily simplified description of the project.

My question is, what is the best practices for implementing a class with child classes like this? Should I just create a library that requires() all child classes? Or can I actually use models for this? Could I auto-load the superclass Container, and then in my code say something like this pseudo code:

model/rack.php
class Rack extends Container {
..
}

application/foo.php
function show {
$this->load->model('types'/.$type, 'object');
$html = $this->object->render();
$this->load->view($html);
}

Is this model approach the way to go?




Theme © iAndrew 2016 - Forum software by © MyBB