Jump to content

[SOLVED] Class extends


MadTechie

Recommended Posts

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 />')));
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.