In some of my recent code I found it desirable to obtain class hierarchy
info without having to create a class instance.  I just made this and it
was the first time I have really looked at the php src, hope it dosent
break anything ;) 

<?
class A {
        var $a = 0;
        
        function A($a = 0) {
                $this->a = $a;
        }
}

class B extends A {

}

$a =& new A;
$b =& new B;

function test($x, $y) {
        $tfs = (is_subclass_of($x,$y))?"true":"false";
        $tfa = (is_a($x,$y))?"true":"false";
        
        if(is_object($x)) {
                $a = "<strong>obj:".get_class($x)."</strong>";
        } else {
                $a = "<strong>str:$x</strong>";
        }
        if(is_object($y)) {
                $b = "<strong>obj:".get_class($y)."</strong>";
        } else {
                $b = "<strong>str:$y</strong>";
        }
        
        echo $a." is_subclass_of ".$b.": ".$tfs."<br>\n".$a." is_a ".$b.":
".$tfa."<br><br>\n";
}

test($a, "A");
test($a, $a);
test("A", $a);
test("A", "A");

test($b, "A");
test($b, $a);
test("B", $a);
test("B", "B");
?>


Produces:


obj:a is_subclass_of str:A: false
obj:a is_a str:A: true

obj:a is_subclass_of obj:a: false
obj:a is_a obj:a: true

str:A is_subclass_of obj:a: false
str:A is_a obj:a: true

str:A is_subclass_of str:A: false
str:A is_a str:A: true

obj:b is_subclass_of str:A: true
obj:b is_a str:A: true

obj:b is_subclass_of obj:a: true
obj:b is_a obj:a: true

str:B is_subclass_of obj:a: true
str:B is_a obj:a: true

str:B is_subclass_of str:B: false
str:B is_a str:B: true


Rodric Glaser
Senior Programmer/Analyst
rglaser -at- propertyline -dot- com

Attachment: is_a_impl-objstr.diff.gz
Description: GNU Zip compressed data

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to