Skip to content

Instantly share code, notes, and snippets.

@Stenerson
Created April 7, 2015 16:17
Show Gist options
  • Save Stenerson/0d79a2ee82a2096f7411 to your computer and use it in GitHub Desktop.
Save Stenerson/0d79a2ee82a2096f7411 to your computer and use it in GitHub Desktop.
PHP Public Methods
<?php
class Class_Utility {
// Echoes headings for each class with an unordered list of public methods
public static function echo_subclasses_and_methods($parent, $exclude_classes = null, $exclude_methods = null)
{
// Loop through results to echo "pretty" output
foreach (static::get_subclasses_and_methods($parent, $exclude_classes, $exclude_methods) as $class) {
echo "<h2>".$class['name']."</h2>";
echo "<ul>";
foreach ($class['methods'] as $method) {
echo "<li>".$method."</li>";
}
echo "</ul>";
}
}
// Returns an array of all subclasses and methods of a given class
// excluding any classes or methods specified by the last two optional parameters
public static function get_subclasses_and_methods($parent, $exclude_classes = null, $exclude_methods = null)
{
$exclude_classes = is_array($exclude_classes) ? $exclude_classes : array();
$exclude_methods = is_array($exclude_methods) ? $exclude_methods : array();
$subclasses = static::get_subclasses($parent);
$result = array();
foreach ($subclasses as $class) {
if (!in_array($class, $exclude_classes)) {
$tmp = array();
$tmp['name'] = $class;
$tmp['methods'] = static::get_public_methods($class, $exclude_methods);
$result[] = $tmp;
}
}
return $result;
}
// Returns array of subclasses given a parent class
// All classes must be loaded prior to calling this function
public static function get_subclasses($parent)
{
$result = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, $parent))
$result[] = $class;
}
return $result;
}
// Returns an array containing the public methods of a given class
// excluding the methods specified by the optional second parameter
public static function get_public_methods($class, $exclude = null)
{
$exclude = is_array($exclude) ? $exclude : array();
$result = array();
$reflect = new ReflectionClass($class);
$methods = $reflect->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
if ($method->class == $class && !in_array($method->name, $exclude)) {
$result[] = $method->name;
}
}
return $result;
}
} // end of Class_Utility class
<h2>Reports</h2>
<ul>
<li>index</li>
<li>orders</li>
<li>textures</li>
<li>food</li>
</ul>
<h2>Food</h2>
<ul>
<li>index_get</li>
<li>save_as_post</li>
<li>audio_post</li>
<li>audio_delete</li>
<li>image_post</li>
<li>image_delete</li>
<li>rating_get</li>
<li>rating_post</li>
</ul>
<h2>Orders</h2>
<ul>
<li>details_get</li>
<li>details_post</li>
<li>comment_post</li>
</ul>
<?php
/****************************
Usage Example - I'm using CodeIgniter and want to view public controller methods (i.e. HTTP endpoints)
****************************/
// Load the Class_Utility Class
$this->load->library('Class_Utility');
// Make sure all contollers are loaded
foreach (array_merge(glob(APPPATH."controllers/*.php"),glob(APPPATH."controllers/_api/*.php")) as $filename)
{
include_once $filename;
}
// Set the class we want to examine
$parent = 'CI_Controller';
// Set any classes you'd like to exclude (I'm excluding super classes)
$exclude_classes = array('LB_Controller','LBREST_Controller','REST_Controller');
// Set any methods you'd like to exclude (I exclude constructors and remaps)
$exclude_methods = array('__construct','_remap');
// Call the echo function
Class_Utility::echo_subclasses_and_methods($parent, $exclude_classes, $exclude_methods);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment