Jump to content

Object oriented OOP module class


Redroest
 Share

Recommended Posts

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

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

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

@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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...