post Category: Uncategorized post postFebruary 21, 2008

This Article was originally written by :

 

 

OK, here’s a scenario:
You are developing a Content Managment System. You have some information that is available to the public. New information, however, is kept in a sandbox until it is approved by management.You have a class for public information, and a class for private information. You want to use the same method for previewing your sandbox information as you do for formating your public information.
Solutions:
1. Take the easy way out. Copy the method from public_information to sandbox_information. This way has serious disadvatages. You are copying code instead of reusing it. That’s not cool. Also, if you change the public_information::format method, you have to manualy update the sandbox_information::preview method.

’.$this->info.’’;
   }
}
class sandbox_information {
   private $info = ‘Sandbox’;
   function preview() {
      return ‘’.$this->info.’’;
   }
}
   $pub = new public_information;
   echo $pub->format();
   $san = new sandbox_information;
   echo $san->preview();
?>

2. Use a parent/child relationship. Have an information class and extend it. Take careful note to use protected and not private propeties. This is a great solution, but may not be possible.

’.$this->info.’’;
		}
	}

	class public_information extends information {
		protected $info = ‘Public’;
	}

	class sandbox_information extends information {
		protected $info = ‘Sandbox’;
		function preview() {
			return $this->format();
		}
	}
	$pub = new public_information;
	echo $pub->format();
	$san = new sandbox_information;
	echo $san->preview();
?>

3. Let’s say you can’t do number 2 because the classes are already children. PHP5 allows you to make a generic call to a method of a different class. If you do this inside a method, you can even use $this. But wait, you say, the manual says you can’t make a static method call to a method that uses $this. It turns out that that’s only true if you are doing it when not in object context. When you do it from a class method, you are in object context. (NOTE, this may be considered a BUG. PHP6 will send an ERROR_FAILURE on this when it comes out.)

’.$this->info.’’;
		}
	}

	class sandbox_information extends sandbox_classes{
		public $info = ‘Sandbox’;
		function preview() {
			return public_information::format();
		}
	}
	$pub = new public_information;
	echo $pub->format();
	$san = new sandbox_information;
	echo $san->preview();
	// Fatal error: Using $this when not in object context
	//public_information::format();
?>

Related Articles


Tags:



 

Sorry, no comments yet.

Write Your Comment

Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs will be generated automatically.

You should have a name, right? 
Your email address, I promised I won't tell it to anyone. 
If you have a web site or blog, you can type the URL right here. 
This is where you type your comments. 
Sum of three plus four ?
Remember my information for the next time I visit.
 
Directory of Web Development Blogs