Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

9/1/2021 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.

6.5.1. Examining Classes

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( );

Send feedback You can get the methods and properties


that exist in a class (including those that are inherited from
superclasses) using the
get_class_methods(
) and get_class_vars(
) functions. These functions take a class name and return
an array:
Why this ad? 
$methods = get_class_methods(classname);
Hiring Cloud
$properties = get_class_vars(classname);

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

$methods = get_class_methods(Person); // same


Product
$methods = get_class_methods('Person'); // same
Implementation

Specialists with
The array returned by get_class_methods( ) is a
simple list of method names. The associative array returned by
4+ years of

get_class_vars( ) maps property names to values


and also includes inherited properties. One quirk of
get_class_vars(
experience to

) is that it returns only


properties that have default values; there's no way
to discover uninitiailized properties.
join us
Use get_parent_class( ) to find a
class's parent class:

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.

Example 6-1. Displaying all declared classes

Open function display_classes ( ) {


$classes = get_declared_classes( );
foreach($classes as $class) {
echo "Showing information about $class<br />";

echo "$class methods:<br />";


$methods = get_class_methods($class);
if(!count($methods)) {
echo "<i>None</i><br />";
}
else {
foreach($methods as $method) {
echo "<b>$method</b>( )<br />";
}
}

echo "$class properties:<br />";


$properties = get_class_vars($class);
if(!count($properties)) {
echo "<i>None</i><br />";
}
else {
foreach(array_keys($properties) as $property) {
echo "<b>\$$property</b><br />";
}
} Ads by
echo "<hr />"; Send feedback Why this ad? 
}
}

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

Why this ad?  Figure 6-1. Output of display_classes( )

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.

Just as get_class_vars( ) returns an array of


properties for a class, get_object_vars(
)
returns an array of properties
Hexaware

Technologies Ltd
set in
an object:

$array = get_object_vars(object);

And just as get_class_vars( ) returns only those


properties with default values, get_object_vars( )
returns only those
properties that are set:

Open class Person {


var $name;
var $age;
}
$fred = new Person;
$fred->name = 'Fred';
$props = get_object_vars($fred); // $props is array('name' => 'Fred');

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

6.5.3. Sample Introspection Program

Example 6-2 shows a collection of functions that display a


reference page of information about an object's
properties,
methods, and inheritance tree.

Example 6-2. Object introspection functionsAds by


Send feedback
// return an array of callable Why thisinherited
methods (include ad?  methods)
function get_methods($object) {
$methods = get_class_methods(get_class($object));

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;
}

// return an array of inherited methods


function get_inherited_methods($object) {
$methods = get_class_methods(get_class($object));

if(get_parent_class($object)) {
$parent_methods = get_class_methods(get_parent_class($object));
$methods = array_intersect($methods, $parent_methods);
}

return $methods;
}

// return an array of superclasses


function get_lineage($object) {
if(get_parent_class($object)) {
$parent = get_parent_class($object);
$parent_object = new $parent;

$lineage = get_lineage($parent_object);
$lineage[] = get_class($object);
Ads by }
else {
$lineage = array(get_class($object));
Send feedback }

Why this ad?  return $lineage;


}

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>";

Open echo '<h2>Inheritance</h2>';

echo '<h3>Parents</h3>';
$lineage = get_lineage($object);
array_pop($lineage);
echo count($lineage) ? ('<p>' . join(' -&gt; ', $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 />';
}
}

echo '<hr />';


}

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( ) { }
};

Ads by class C extends B {


};

$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

Figure 6-2. Object introspection output

6.4. Declaring a Class Ads by 6.6. Serialization

Send feedback Why this ad? 

https://docstore.mik.ua/orelly/webprog/php/ch06_05.htm 4/5
9/1/2021 Introspection (Programming PHP)

Copyright © 2003 O'Reilly & Associates. All rights reserved.

Ads by
Send feedback Why this ad? 
AWS Jobs
Hiring talents with 4+ years of experience to
Implementation Specialist

Ads by Hexaware Technologies Ltd

Send feedback

Why this ad? 

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

You might also like