Welcome Guest, Not a member yet? Register   Sign In
Creating OOP classes in a right way
#1

[eluser]Unknown[/eluser]
Hello,
My problem is how to right Create OOP classes in codeigniter
For example:
Code:
Class forum{
   Public $id;
   Public $name;
   Public $topics = new Topics($this->id); // forum has topics ..

  
}


Class Topics{
   Public $id;
   public $topicname;
   public $reactions;


}
Where can i place and Call a complete set of OOP classes who called to each others?
So i need to have à class: forum 'has' topics 'has' reactions etc...
#2

[eluser].giorgio[/eluser]
also for me this has been quite a problem... I'd like to write my code as much OOP as possible, for the sake of simplicity at the semantic level. I'd rather iterate through objects of, say, a Topic, than iterate through database results (although in the end the same result could be accomplished). I'm eagerly looking for a pattern to implement such a thing using codeigniter and Models (or don't I understand the model principle?). Especially for inheritence and abstraction Models don't suit my needs...

How could i accomplish something like this?

Code:
abstract class Figure {
  protected name;
  protected location = array('x' => 2, 'y' => 10);

  abstract function get_center();
}

class Circle extends Figure {
  private $radius = 4;

  public function get_center() {
    $x = $this->location['x']+$this->radius;
    $y = $this->location['y']+$this->radius;
    return array('x' => $x, 'y' => $y);
  }
}

class Square extends Figure {
  private $width;
  private $height;

  public function get_center() {
    $x = $this->location['x']+($this->width/2);
    $y = $this->location['y']+($this->height/2);
    return array('x'=>$x, 'y'=>$y);
  }
}

$figures = array(
  new Circle,
  new Square,
  new Square,
  new Circle
);

foreach($figures as $figure) {
  $center = $figure->get_center();
  echo 'This '. get_class($figure) .'\'s center is located at: ['.$center['x'].','. $center['y'].']<br />';
}




Theme © iAndrew 2016 - Forum software by © MyBB