Introspection (Programming PHP)
Introspection (Programming PHP)
home |
O'Reilly's CD bookshelfs |
FreeBSD | Linux | Cisco |
Cisco Exam
6.5. Introspection
Ads by
Introspection
is the ability of a program to examine an object's
characteristics, such as its name, parent class (if any),
properties,
and methods. With introspection, you can write code that operates on
any class or object. You don't need to know
Send feedba
which
methods or properties are defined when you write your code; instead,
you can discover that information at runtime,
which makes it possible
for you to write generic debuggers, serializers, profilers, etc. In
this section, we look at the
Why this ad?
introspective functions provided by PHP.
To determine
whether a class exists, use the class_exists(
)
function, which takes in a
string and returns a Boolean value.
Alternately, you can use the
get_declared_classes(
) function, which returns an
array of defined classes and checks if
the class name is in the
returned array:
Ads by $yes_no = class_exists(classname);
$classes = get_declared_classes( );
Professionals The class name can be a bare word, a quoted string, or a variable
containing the class name:
$class = 'Person';
$methods = get_class_methods($class);
We’re calling
Specialists with
The array returned by get_class_methods( ) is a
simple list of method names. The associative array returned by
4+ years of
Hexaware
$superclass = get_parent_class(classname);
Technologies Ltd
Example 6-1 lists the display_classes(
)
function,
which displays all currently declared classes and the methods and
properties for each.
https://docstore.mik.ua/orelly/webprog/php/ch06_05.htm 1/5
9/1/2021 Introspection (Programming PHP)
Figure 6-1 shows the output of the
display_classes( ) function.
Ads by
Send feedback
Hiring Cloud
6.5.2. Examining an Object
Professionals
To get the class to which an object belongs,
first make sure it is an object using the is_object(
) function, then get the class
with the
get_class( ) function:
We’re calling
$yes_no = is_object(var);
Product
$classname = get_class(object);
Implementation
Specialists with
Before calling a method on an object, you can ensure that it exists
using the method_exists(
) function:
4+ years of
experience to
$yes_no = method_exists(object, method);
join us
Calling an undefined method triggers a runtime exception.
Technologies Ltd
set in
an object:
$array = get_object_vars(object);
The get_parent_class(
) function actually accepts
either an object or a class name. It returns the name of the parent
class,
or FALSE if there is no parent class:
class A {}
class B extends A {}
$obj = new B;
echo get_parent_class($obj); // prints A
echo get_parent_class(B); // prints A
https://docstore.mik.ua/orelly/webprog/php/ch06_05.htm 2/5
9/1/2021 Introspection (Programming PHP)
if(get_parent_class($object)) {
$parent_methods = get_class_methods(get_parent_class($object));
$methods = array_diff($methods, $parent_methods);
}
return $methods;
}
if(get_parent_class($object)) {
$parent_methods = get_class_methods(get_parent_class($object));
$methods = array_intersect($methods, $parent_methods);
}
return $methods;
}
$lineage = get_lineage($parent_object);
$lineage[] = get_class($object);
Ads by }
else {
$lineage = array(get_class($object));
Send feedback }
Hiring Cloud
// return an array of subclasses
Professionals function get_child_classes($object) {
$classes = get_declared_classes( );
$children = array( );
We’re calling
foreach($classes as $class) {
Product
if (substr($class, 0, 2) == '_ _') {
Implementation
continue;
}
Specialists with
$child = new $class;
4+ years of
if(get_parent_class($child) == get_class($object)) {
experience to
$children[] = $class;
join us }
}
return $children;
Hexaware
}
Technologies Ltd
// display information on an object
function print_object_info($object) {
$class = get_class($object);
echo '<h2>Class</h2>';
echo "<p>$class</p>";
echo '<h3>Parents</h3>';
$lineage = get_lineage($object);
array_pop($lineage);
echo count($lineage) ? ('<p>' . join(' -> ', $lineage) . '</p>')
: '<i>None</i>';
echo '<h3>Children</h3>';
$children = get_child_classes($object);
echo '<p>' . (count($children) ? join(', ', $children)
: '<i>None</i>') . '</p>';
echo '<h2>Methods</h2>';
$methods = get_class_methods($class);
$object_methods = get_methods($object);
if(!count($methods)) {
echo "<i>None</i><br />";
}
else {
echo '<p>Inherited methods are in <i>italics</i>.</p>';
foreach($methods as $method) {
echo in_array($method, $object_methods) ? "<b>$method</b>( );<br />"
: "<i>$method</i>( );<br />";
}
} Ads by
Send feedback
echo '<h2>Properties</h2>'; Why this ad?
$properties = get_class_vars($class);
if(!count($properties)) {
echo "<i>None</i><br />";
https://docstore.mik.ua/orelly/webprog/php/ch06_05.htm 3/5
9/1/2021 Introspection (Programming PHP)
}
else {
foreach(array_keys($properties) as $property) {
echo "<b>\$$property</b> = " . $object->$property . '<br />';
}
}
Here are some sample classes and objects that exercise the
introspection functions from Example 6-2:
class A {
var $foo = 'foo';
var $bar = 'bar';
var $baz = 17.0;
function first_function( ) { }
function second_function( ) { }
};
class B extends A {
var $quux = false;
function third_function( ) { }
};
$a = new A;
Send feedback $a->foo = 'sylvie';
$a->bar = 23;
Why this ad?
$b = new B;
Hiring Cloud
$b->foo = 'bruno';
$b->quux = true;
Professionals
$c = new C;
print_object_info($a);
We’re calling
print_object_info($b);
Product
print_object_info($c);
Implementation
Specialists with
Figure 6-2 shows the
output of this code.
4+ years of
experience to
join us
Hexaware
Technologies Ltd
Open
https://docstore.mik.ua/orelly/webprog/php/ch06_05.htm 4/5
9/1/2021 Introspection (Programming PHP)
Ads by
Send feedback Why this ad?
AWS Jobs
Hiring talents with 4+ years of experience to
Implementation Specialist
Send feedback
Hiring Cloud
Professionals
We’re calling
Product
Implementation
Specialists with
4+ years of
experience to
join us
Hexaware
Technologies Ltd
Open
Ads by
Send feedback Why this ad?
https://docstore.mik.ua/orelly/webprog/php/ch06_05.htm 5/5