Importing Methods from an unrelated Class in PHP5

Importing Methods from an unrelated Class in PHP5

By Andrew Penry

Posted on February 28th, 2005

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.
<?php
class public_information {
private $info = 'Public';
function format() {
return '<b>'.$this->info.'</b>';
}
}
class sandbox_information {
private $info = 'Sandbox';
function preview() {
return '<b>'.$this->info.'</b>';
}
}
$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.
<?php
class information {
protected $info = '';
function format() {
return '<b>'.$this->info.'</b>';
}
}
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.)
<?php
class public_classes {}
class sandbox_classes {}
class public_information extends public_classes{
private $info = 'Public';
function format() {
return '<b>'.$this->info.'</b>';
}
}
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();
?>
Slick.
Copyright © 2005-2009 by Andrew Penry.
sitemap RGB Color Columbus Photography Free Word Search Maker monty hall game Hangman John Dewey