return an array of callable methods include inherited methods function

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
// return an array of callable methods (include inherited methods)
function get_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_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);
}
else {
$lineage = array(get_class($object));
}
return $lineage;
}
// return an array of subclasses
function get_child_classes($object) {
$classes = get_declared_classes( );
$children = array( );
foreach($classes as $class) {
if (substr($class, 0, 2) == '_ _') {
continue;
}
$child = new $class;
if(get_parent_class($child) == get_class($object)) {
$children[] = $class;
}
}
return $children;
}
// display information on an object
function print_object_info($object) {
$class = get_class($object);
echo '<h2>Class</h2>';
echo "<p>$class</p>";
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 />";
}
}
echo '<h2>Properties</h2>';
$properties = get_class_vars($class);
if(!count($properties)) {
echo "<i>None</i><br />";
}
else {
foreach(array_keys($properties) as $property) {
echo "<b>\$$property</b> = " . $object->$property . '<br />';
}
}
echo '<hr />';
}