# Best OOP method for a layout library

I had a code for a layout library which looked like :

$this->layout->setLayout($layout)->setTitle($title)->render() etc.  Today I started writing a new code, simpler and smaller : $this->layout->data(array(


The previous setters/add methods were like :

public function setDoctype($doctype) {$this->doctype = $doctype; return$this;
}

public function addJs($name) { if(!in_array($name, $this->js)) {$this->js[] = $name; } return$this;
}


The new data method :

public function data($data = array()) { if(isset($data['view_name'])) $this->ci->config->load($data['view_name']);
else $this->ci->config->load($this->data['view_name']);

// Mark as forbid because are stored in arrays and
// I can add values to those
$forbid = array('css', 'js', 'rss'); foreach($this->ci->config->config['layout'] as $key =>$value) {
if(isset($data[$key]) && !in_array($key,$forbid))
$this->data[$key] = $data[$key];
else
$this->data[$key] = $value; if(isset($data[$key]) && in_array($key, $forbid)) { if(is_array($data[$key])) { if(count($array) != count($array, COUNT_RECURSIVE)) foreach($data[$key] as$key2 => $value2)$this->data[$key][$key2] = $value2; else foreach($data[$key] as$value2)
$this->data[$key][] = $value2; } else$this->data[$key][] =$data[$key]; } } return$this;
}


I changed to this because of the many things that I can update for every page. And use ->setThing would be annoying. But I don't find that my code is so OOP.

Note that I'm using CodeIgniter.

What's the best oop code ? If you want more indications, please tell me I'm new and don't know how to say more ;) .

• You should add more details, especially code of your layout class. – Sven Apr 19 '13 at 23:48
• It is done :) . – Cxodael Apr 20 '13 at 0:21
• I like ->setThing, because i can use type ahead, also you can easily make a backtrace in the function and you have only backtrace for the manipulations of Thing and not for every other data in your class. – AlucardTheRipper Apr 20 '13 at 12:16
• Your paths should start with '/', i.e /admin/index. So that they start at the root of the domain (especially if using mod_rewrite) – Eddie Jaoude May 13 '13 at 16:51
• As John said, it's very effective. It's common practice in many frameworks... and in Code Igniter itself! – Quentin Pradet May 17 '13 at 6:48