Awilum Posted November 22, 2010 Share Posted November 22, 2010 Hello) I write html "library" for my php App in function style. But I think in OOP style will be better What do think about first and second variant. Maybe I wrote something wrong in OOP style ? /** * Create a link in button style. * @param string $txt name * @param stting $url link * @param string $title title if is empty then title = $txt * @param boolean $render if this option is true then render html object else return it */ if (!function_exists('uiButton')) { function uiButton($txt,$url,$title=NULL,$render=TRUE) { if(empty($title)) $title = $txt; // If $output is true then render else return $output if($render) echo '<span class="btn"><a href="'.$url.'" title="'.$title.'">'.$txt.'</a></span>'; else return $output; } } /** * Create a href link * @param string $txt name * @param stting $url link * @param string $title title if is empty then title = $txt */ if (!function_exists('uiLink')) { function uiLink($txt,$url,$title=NULL,$render=TRUE) { if(empty($title)) $title = $txt; $output = '<a href="'.$url.'" title="'.$title.'">'.$txt.'</a>'; // If $output is true then render else return $output if($render) echo $output; else return $output; } } if (!function_exists('uiHeading')) { function uiHeading($txt,$h='1',$render=TRUE) { $output = '<h'.$h.'>'.$txt.'</'.$h.'>'; if($render) echo $output; else return $output; } } if (!function_exists('uiBr')) { function uiBr($num = 1) { echo str_repeat("<br />", $num); } } if (!function_exists('uiNbsp')) { function uiNbsp($num = 1) { echo str_repeat(" ", $num); } } if (!function_exists('uiImg')) { function uiImg($src,$align=NULL,$alt=NULL,$border=NULL,$width=NULL,$height=NULL,$render=TRUE) { $output .= '<img '; $output .= 'src="'.$src.'" '; if(!empty($align)) $output .= 'align="'.$align.'" '; if(!empty($border)) $output .= 'border="'.$botder.'" '; if(!empty($width)) $output .= 'width="'.$width.'" '; if(!empty($height)) $output .= 'height="'.$height.'" '; if(!empty($alt)) $output .= 'alt="'.$alt.'" '; else $output .= 'alt=""'; $output .= ' />'; // If $output is true then render else return $output if($render) echo $output; else return $output; } } exemple first variant uiImg('http://site.com/img/img.png','','','',640,480,$render=TRUE) { and second variant class HTML { var $name; var $url; var $action; var $method = 'post'; var $width; var $hight; var $alt; var $title; var $align; var $class; var $id; var $style; var $border; var $value; function link() { echo '<a href='.$this->url.'>'.$this->name.'</a>'; } function formOpen() { echo '<form action="'.$this->action.'" method="'.$this->method.'">'; } function img() { $output = ''; $output .= '<img '; $output .= 'src="'.$this->url.'" '; if(!empty($this->align)) $output .= 'align="'.$this->align.'" '; if(!empty($this->border)) $output .= 'border="'.$this->border.'" '; if(!empty($this->width)) $output .= 'width="'.$this->width.'" '; if(!empty($this->height)) $output .= 'height="'.$this->height.'" '; if(!empty($this->alt)) $output .= 'alt="'.$this->alt.'" '; else $output .= 'alt=""'; $output .= ' />'; echo $output; } } $img = new HTML(); $img->url = 'http://site.com/img/img.png'; $img->width = 640; $img->height = 480; $img->img(); Quote Link to comment https://forums.phpfreaks.com/topic/219457-html-lib-in-functional-or-oop-style/ Share on other sites More sharing options...
Awilum Posted November 23, 2010 Author Share Posted November 23, 2010 my OOP variant is good or bad ? may be it can be better ? Quote Link to comment https://forums.phpfreaks.com/topic/219457-html-lib-in-functional-or-oop-style/#findComment-1138372 Share on other sites More sharing options...
trq Posted November 23, 2010 Share Posted November 23, 2010 The idea should be to isolate html from php, not combine them. Quote Link to comment https://forums.phpfreaks.com/topic/219457-html-lib-in-functional-or-oop-style/#findComment-1138650 Share on other sites More sharing options...
Awilum Posted November 23, 2010 Author Share Posted November 23, 2010 thorpe for exemple this function is helpful function uiBr($num = 1) { echo str_repeat("<br />", $num); } uiBr(7); better then <br /><br /><br /><br /><br /><br /><br /> qustion about my OOP style. Bad or normal ? class HTML { var $name; var $url; var $action; var $method = 'post'; var $width; var $hight; var $alt; var $title; var $align; var $class; var $id; var $style; var $border; var $value; function link() { echo '<a href='.$this->url.'>'.$this->name.'</a>'; } function formOpen() { echo '<form action="'.$this->action.'" method="'.$this->method.'">'; } function img() { $output = ''; $output .= '<img '; $output .= 'src="'.$this->url.'" '; if(!empty($this->align)) $output .= 'align="'.$this->align.'" '; if(!empty($this->border)) $output .= 'border="'.$this->border.'" '; if(!empty($this->width)) $output .= 'width="'.$this->width.'" '; if(!empty($this->height)) $output .= 'height="'.$this->height.'" '; if(!empty($this->alt)) $output .= 'alt="'.$this->alt.'" '; else $output .= 'alt=""'; $output .= ' />'; echo $output; } } $img = new HTML(); $img->url = 'http://site.com/img/img.png'; $img->width = 640; $img->height = 480; $img->img(); if you know the better variants, offer) Quote Link to comment https://forums.phpfreaks.com/topic/219457-html-lib-in-functional-or-oop-style/#findComment-1138697 Share on other sites More sharing options...
trq Posted November 23, 2010 Share Posted November 23, 2010 Well formed html should never need multiple line breaks one after the other. qustion about my OOP style. Bad or normal ? Just because you have stuck all your functions in a class does not make it OOP. Quote Link to comment https://forums.phpfreaks.com/topic/219457-html-lib-in-functional-or-oop-style/#findComment-1138712 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.