Traxus Posted July 23, 2010 Share Posted July 23, 2010 As of yesterday, I have just started playing with classes, I'm a complete greenhorn regarding OOP. I'm in the midst of building myself a simple templating engine. I have a question regarding a common function (method?) named EchoIfSet that appears in two classes: function EchoIfSet ($open_tag, $file_src, $close_tag) { if (isset($file_src)){ echo "\n".$open_tag.$file_src.$close_tag; } } I know there is a more efficient way to use this in both classes: <?php class HtmlHead { var $Keywords; var $Description; var $PageStyles; var $PageScripts; //------------------------ var $SectionStyles; var $SectionScripts; //------------------------ var $SiteStyles; var $SiteScripts; //------------------------ var $CatchAll; function Constructor() { echo "\n<head>"; echo "\n<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />"; echo "\n<meta name='keywords' content='".$this->Keywords."' />"; echo "\n<meta name='description' content='".$this->Description."' />"; //------------------------ $this->EchoIfSet("<script src='", $this->SiteScripts, "' type='text/javascript'></script>"); $this->EchoIfSet("<script src='", $this->SectionScripts, "' type='text/javascript'></script>"); $this->EchoIfSet("<script src='", $this->PageScripts, "' type='text/javascript'></script>"); //------------------------ $this->EchoIfSet("<link rel='stylesheet' type='text/css' href='", $this->SiteStyles, "' />"); $this->EchoIfSet("<link rel='stylesheet' type='text/css' href='", $this->SectionStyles, "' />"); $this->EchoIfSet("<link rel='stylesheet' type='text/css' href='", $this->PageStyles, "' />"); $this->EchoIfSet(NULL, $this->CatchAll, NULL); echo "\n</head>"; } function EchoIfSet ($open_tag, $file_src, $close_tag) { if (isset($file_src)){ echo "\n".$open_tag.$file_src.$close_tag; } } } ?> <?php //this one will be called several times on the page class HtmlBlock { var $DivId; var $DivClass; var $DivContent; function Constructor() { //------------------------ echo "\n<div"; $this->EchoIfSet(" id='", $this->DivId, "'"); $this->EchoIfSet(" class='", $this->DivClass, "'"); echo ">"; //------------------------ $this->EchoIfSet(NULL, $this->DivContent, NULL); //------------------------ echo "\n</div>"; } function EchoIfSet ($open_tag, $file_src, $close_tag) { if (isset($file_src)){ echo "\n".$open_tag.$file_src.$close_tag; } } } ?> But I'm not sure where to look for documentation on this. I did some googling/rtfm regarding subclasses, but all of that documentation seems to be in regards to abstract classes, and if i understand correctly abstract classes should have no functions or variables actually defined in them, its basically a template for other classes... Can anyone point me towards a tutorial that deals with class hierarchies? Link to comment https://forums.phpfreaks.com/topic/208705-help-with-class-hiearchy-common-functions-between-classes/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.