Home > Web > PHP >

Importing Methods from an unrelated Class in PHP5

Posted Feb 27, 2005
Last Updated Aug 13, 2013

NOTE: Don’t ever do this. I’m leaving this article up for archival purposes. But with the introduction of traits in PHP 5.4, there is a real, tested way of doing mix-ins that doesn’t rely on what may be considered a bug. See PHP Traits for Implementing Interfaces for more information.


OK, here’s a scenario:
You are developing a Content Management 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 formatting your public information.

Solutions:

  • Take the easy way out. Copy the method from public_information to sandbox_information. This way has serious disadvantages. You are copying code instead of reusing it. That’s not cool. Also, if you change the public_information::format method, you have to manually 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 '**'.$this->info.'**'; 
    }
} 

$pub = new public_information;
echo $pub->format();
$san = new sandbox_information;
echo $san->preview();
  • Use a parent/child relationship. Have an information class and extend it. Take careful note to use protected and not private properties. 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();
  • Let’s say you can’t do number 2 because the classes are already children. PHP5 allows you to make a static 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.

Comment

No HTML Tags are permitted.

Wall Worm plugins and scripts for 3ds Max