MadTechie Posted April 20, 2007 Share Posted April 20, 2007 OK basically i have a class that is growing quite large.. i have look and found some posts close but not exacly what i need.. Now i have been extending the class but its a pain also i don't need all the extended classes all the time so Question #1 Does the this type of code decrease performance in any way? Question #2 is their a better way of doing this? for example if i didn't need MoreTextStuff but could add it in half way through a routine Sample code (very basic) <?php $Test = new TextFormatting; $Test->AddText("Test<br />"); $Test->Underline(); $Test->AddText("Test2<br />"); $Test->BoldText(); $Test->AddText("Test3<br />"); echo $Test->TheText; class TextStuff { public $TheText; function AddText($string) { $this->TheText = $this->TheText.$string; } } class MoreTextStuff extends TextStuff { function BoldText() { $this->TheText = "<u>".$this->TheText."</u>"; } } class TextFormatting extends MoreTextStuff { function Underline() { $this->TheText = "<b>".$this->TheText."</b>"; } } ?> any ideas, Thanks In advance --MadTechie Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 20, 2007 Author Share Posted April 20, 2007 *BUMP* Suggection even ? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 21, 2007 Author Share Posted April 21, 2007 *BUMP#2* I am starting to think i am on my own on this one! Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 21, 2007 Author Share Posted April 21, 2007 last bump... ~Sighs~ will check when i get up.. i dream of a solution or an idea Quote Link to comment Share on other sites More sharing options...
emehrkay Posted April 21, 2007 Share Posted April 21, 2007 whats wrong with having all of those types of functions in a single class? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 22, 2007 Author Share Posted April 22, 2007 First off, Thank you for replying (was lossing hope) OK basically i have a class thats getting quite large, and i don't know if the way i am doing it affects performance, taking into account that i don't need two thirds of the classes half the time, the problem with adding them all into one class is that it would get very messy (more then now) each class uses its own variables and private methods where function names would conflict. i guess if i could include the class with "these" extended classes or some how append a class to it after its been included.. that might be a way but again would this affect performance ! kinda at a loss end if you know what i mean! Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted April 22, 2007 Share Posted April 22, 2007 I would suggest making the class static, like I've done below. Having a large class isn't that big of a deal if you don't have an instance sitting on the heap. Using a static class prevents this. <?php $out = TextStuff::underlineText("Test<br />") . TextStuff::boldText("Test2<br />") . "Test3<br />"; echo $out; class TextStuff { // Don't allow this class to be instantiated. private function __construct(){} public static function boldText($string) { return "<b>".$string."</b>"; } public static function underlineText($string) { return "<u>".$string."</u>"; } } ?> Best, Patrick Quote Link to comment Share on other sites More sharing options...
448191 Posted April 25, 2007 Share Posted April 25, 2007 You can use this polymorphic version of the Decorator pattern to instantiate specific formatting objects, as well as assembling specialized (combined) formatting objects: <?php class FormattedString { private $string; public function __construct($string){ $this->string = $string; } public function getString(){ return $this->string; } public function __toString(){ return $this->getString(); } } abstract class FormattedStringDecorator extends FormattedString { protected $formattedString; public function __construct($formattedString){ if($formattedString instanceof FormattedString){ $this->formattedString = $formattedString; } else { $this->formattedString = new FormattedString($formattedString); } } } class BoldString extends FormattedStringDecorator { public function getString(){ return "<strong>".$this->formattedString->getString()."</strong>"; } } class UnderlinedString extends FormattedStringDecorator { public function getString(){ return "<u>".$this->formattedString->getString()."</u>"; } } echo new UnderlinedString("Test<br />"); echo new BoldString("Test2<br />"); echo new BoldString(new UnderlinedString(new FormattedString('Test<br />'))); ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Author Share Posted April 25, 2007 ooow that looks cool, thanks emehrkay & utexas_pjm & 448191, i guess its testing time, i don't have time until the weekend but i have some ideas to play with.. will report back note (will leave unsolved as a expect problems) Quote Link to comment 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.