Redroest Posted September 15, 2010 Share Posted September 15, 2010 Hello, I just made an moduleclass which loads files using filelinks and combines it with a layout. I just want to know, is this a correct OOP class? And do you people have any hints on how to make this better? <?phpclass module{ private $controllerFile; private $headerFile; private $title; public function __construct($controllerFile, $headerFile = '', $title) { if(is_file($controllerFile)) { $this->controllerFile = $controllerFile; } if(is_file($headerFile)) { $this->headerFile = $headerFile; } if(is_string($title)) { $this->title = $title; } } public function getTitle() { return $this->title; } public function getControllerFile() { return $this->controllerFile; } public function getHeaderFile() { return $this->headerFile; } public function getBody() { if(isset($this->controllerFile)) { $output = $this->getContent($this->controllerFile); if($output) { return $output; } } } public function getHeader() { if(isset($this->headerFile)) { return module::getContent($this->headerFile); } else { return ''; } } public function getContent($file) { ob_start(); include($file); $content = ob_get_contents(); ob_end_clean(); return $content; }}class container extends module{ public function setLayout($layoutFile) { if(is_file($layoutFile)) { $this->layout = module::getContent($layoutFile); } } public function getOutput() { $body = module::getBody(); $output = preg_replace('/<!-- blockcontent -->/', $body, $this->layout); $output = preg_replace('/<!-- blocktitle -->/', $this->getTitle(), $this->layout); return $output; }}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted September 15, 2010 Share Posted September 15, 2010 In the constructor, it's kind of weird to have a required function parameter after an optional one, but otherwise it looks fine. Link to comment Share on other sites More sharing options...
Redroest Posted September 16, 2010 Author Share Posted September 16, 2010 Cool, so I finally seem to get it now :)But how would you implement the optional parameter in the constructor then? Link to comment Share on other sites More sharing options...
ShadowMage Posted September 16, 2010 Share Posted September 16, 2010 But how would you implement the optional parameter in the constructor then?What jsg is saying is that normally optional parameter come at the end of the list:public function __construct($controllerFile, $title, $headerFile = '') Link to comment Share on other sites More sharing options...
Redroest Posted September 16, 2010 Author Share Posted September 16, 2010 Ah ok, thanks. Its important for me to know that. I never learned php from an education. Its just something I learned in my spare time. Link to comment Share on other sites More sharing options...
music_lp90 Posted September 16, 2010 Share Posted September 16, 2010 public function getTitle() { return $this->title; } public function getControllerFile() { return $this->controllerFile; } public function getHeaderFile() { return $this->headerFile; } Those functions don't seem necessary to me, but maybe I'm missing something. Why have a function to get these values?can't you just do something like this?$obj = new module();echo $obj->title;as opposed to something like this:$obj = new module();$title = $obj->getTitle();echo $title;I'm pretty new to OOP, so maybe there's a good reason for it, but wasn't sure. Link to comment Share on other sites More sharing options...
trevelluk Posted September 17, 2010 Share Posted September 17, 2010 @music_lp90:It's an object-oriented design pattern (in Java its called the "bean" pattern, not sure if there's a formal name for it elsewhere).The key advantage, as I see it, is to help hide implementation details from users of the class and make maintenance easier. For example, maybe in the future someone will decide that titles should always be returned in uppercase. With the getTitle() function, then only one line needs to be changed. Without it, you'd need to check the entire source code of the application. Link to comment Share on other sites More sharing options...
music_lp90 Posted September 17, 2010 Share Posted September 17, 2010 Cool, that makes sense. Thanks, trevelluk. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.