一 函数get_called_class()
//Description
string get_called_class ( void )
Gets the name of the class the static method is called in.
//Report a bug
Return Values
Returns the class name. Returns FALSE if called from outside a class.
Report a bug
Examples
//Example #1 Using get_called_class()
<?php
class foo {
static public function test() { //static method
var_dump(get_called_class());
}
}
class bar extends foo {
}
foo::test();
bar::test();
?>
The above example will output:
string(3) "foo"
string(3) "bar"
二 函数get_parent_class()
<?php
class dad {
function dad()
{
// implements some logic
}
}
class child extends dad {
function child()
{
echo "I'm " , get_parent_class($this) , "'s son\n";
}
}
class child2 extends dad {
function child2()
{
echo "I'm " , get_parent_class('child2') , "'s son too\n";
}
}
$foo = new child();
$bar = new child2();
?>
Result:
I'm dad's son
I'm dad's son too
三 函数get_class()
//Example #1 Using get_class()
<?php
class foo {
function name()
{
echo "My name is " , get_class($this) , "\n";
}
}
// create an object
$bar = new foo();
// external call
echo "Its name is " , get_class($bar) , "\n";
// internal call
$bar->name();
?>
//The above example will output:
Its name is foo
My name is foo
四 函数gettype()
//Example #1 gettype() example
<?php
$data = array(1, 1., NULL, new stdClass, 'foo');
foreach ($data as $value) {
echo gettype($value), "\n";
}
?>
//The above example will output something similar to:
integer
double
NULL
object
string
五 函数is_subclass_of()
//Example #1 is_subclass_of() example
<?php
// define a class
class WidgetFactory
{
var $oink = 'moo';
}
// define a child class
class WidgetFactory_Child extends WidgetFactory
{
var $oink = 'oink';
}
// create a new object
$WF = new WidgetFactory();
$WFC = new WidgetFactory_Child();
if (is_subclass_of($WFC, 'WidgetFactory')) {
echo "yes, \$WFC is a subclass of WidgetFactory\n";
} else {
echo "no, \$WFC is not a subclass of WidgetFactory\n";
}
if (is_subclass_of($WF, 'WidgetFactory')) {
echo "yes, \$WF is a subclass of WidgetFactory\n";
} else {
echo "no, \$WF is not a subclass of WidgetFactory\n";
}
// usable only since PHP 5.0.3
if (is_subclass_of('WidgetFactory_Child', 'WidgetFactory')) {
echo "yes, WidgetFactory_Child is a subclass of WidgetFactory\n";
} else {
echo "no, WidgetFactory_Child is not a subclass of WidgetFactory\n";
}
?>
//The above example will output:
yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory