|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
More about reflection : http://www.weberdev.com/reflection
Usage Exmaple
|
<?php
/*
* First: Some basics about reflection using a sample class. This is
* not advance, only shows the class name and some information:
*/
require_once "reflector.class.php";
$reflector = new ReflectionClass("ReflectorExample");
echo "Class name: ".$reflector->getName()."\n";
echo "Defined in file: ".$reflector->getFileName()."\n";
echo "Between lines: ".$reflector->getStartLine()." and ".$reflector->getEndLine()."\n";
/*
* Now real reflection magic. We implement our own versions of:
* - get_class => my_get_class
* - get_class_methods => my_get_class_methods
* - function_exists => my_function_exists
*
* And a function that retrieves the supported parameters for
* a given function:
* - my_get_function_parameters
*
* Here are placed the output of that functions, so see below
* to read the commented code :-).
*/
/* Get a class name using an instance: */
$my_class = new ReflectorExample();
$classname = my_get_class($my_class);
echo "Classname of \$my_class: $classname\n";
/* Get the class methods */
foreach (my_get_class_methods("ReflectorExample") as $method) {
echo "method found: $method\n";
}
/* Dumps some function parameters: */
foreach (my_function_get_parameters("sprintf") as $parameter) {
echo "Parameter: $parameter\n";
}
/* Shows if functions var_dump, sprintf and foobar are defined: */
$funcs = array("var_dump", "sprintf", "foobar");
foreach ($funcs as $func) {
$exists = my_function_exists($func) ? "yes" : "no";
echo "function $func exists: $exists\n";
}
function my_get_class($instance)
{
$reflector = new ReflectionObject($instance);
if (!$reflector->isInstance($instance))
return false;
return $reflector->getName();
}
function my_get_class_methods($classname)
{
$methods = array();
if (!class_exists($classname))
throw new Exception ("Class $classname does not exists.");
$reflector = new ReflectionClass($classname);
$reflected_methods = $reflector->getMethods();
foreach ($reflected_methods as $reflected)
$methods[] = $reflected->getName();
return $methods;
}
function my_function_exists($function)
{
try {
$reflector = new ReflectionFunction($function);
} catch (ReflectionException $e) {
return false;
}
return true;
}
function my_function_get_parameters($function)
{
if (!my_function_exists($function))
throw new Exception ("Specified function $function does not exists.");
$reflector = new ReflectionFunction($function);
foreach ($reflector->getParameters() as $parameter)
$parameters[] = $parameter->name;
return $parameters;
}
?> | |
reflector.class.php
| <?php
class ReflectorExample
{
public function __construct()
{
}
public function exampleFunction($parameter1)
{
}
}
?> | | |
|
| ECHO-PHP Class Real Time Transaction Processor v1.4.4 for Credit Cards and
Checks / ACH Categories : PHP Classes, Cybercash, Classes and Objects, Ecommerce, PHP | | | PHP CLASS for ORACLE (database connectivity) Categories : PHP, PHP Classes, Classes and Objects, Databases, Oracle | | | Access_user Class - an easy to use system for protecting pages and register users. Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | | | Online Automatic Class Generator for MySQL Tables Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL | | | An efficient iterative and buffered text file reader Categories : PHP, Classes and Objects, Filesystem, PHP Classes, Log Files | | | ClassFuncDoc - This script is a classes and functions documentation tool. Categories : PHP, Classes and Objects, Documentation, PHP Classes, Complete Programs | | | Class: Info on Users, Servers and the running script Categories : PHP, Classes and Objects, User Interface, PHP Classes | | | Mssql database Manager Categories : PHP, Databases, MS SQL Server, Classes and Objects, PHP Classes | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | | | PHP interface class to the eBusiness Charts generatation remote service. Categories : PHP, PHP Classes, Graphics, Charts and Graphs | | | PHP Image Class Categories : PHP, PHP Classes, Multimedia, GD image library | | | Class that allows the PHP developer to establish connections with a POP3 mail server amd be able to list, retrieve and delete mail messages from a given mail box.
Categories : Network, Email, PHP, PHP Classes | | | Export Excel Dynamically to Csv then to mysql Categories : PHP Classes, Excel, PHP | |
|
|