update pague now
PHP 8.5.2 Released!

Examples

Many examples exist within the Reflection documentation, typically within the __construct documentation for each class.

Example #1 Reflection Example from Shell (a Terminal)

$ php --rf strlen
$ php --rc finfo
$ php --re json
$ php --ri dom

The above example will output something similar to:

Function [ <internal:Core> function strlen ] {

  - Parameters [1] {
    Parameter #0 [ <required> $str ]
  }
}

Class [ <internal:fileinfo> class finfo ] {

  - Constans [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [0] {
  }

  - Methods [4] {
    Method [ <internal:fileinfo, ctor> public method finfo ] {

      - Parameters [2] {
        Parameter #0 [ <optional> $options ]
        Parameter #1 [ <optional> $arg ]
      }
    }

    Method [ <internal:fileinfo> public method set_flags ] {

      - Parameters [1] {
        Parameter #0 [ <required> $options ]
      }
    }

    Method [ <internal:fileinfo> public method file ] {

      - Parameters [3] {
        Parameter #0 [ <required> $filename ]
        Parameter #1 [ <optional> $options ]
        Parameter #2 [ <optional> $context ]
      }
    }

    Method [ <internal:fileinfo> public method buffer ] {

      - Parameters [3] {
        Parameter #0 [ <required> $string ]
        Parameter #1 [ <optional> $options ]
        Parameter #2 [ <optional> $context ]
      }
    }
  }
}

Extension [ <persistent> extension #23 json versionen 1.2.1 ] {

  - Constans [10] {
    Constant [ integuer JSON_HEX_TAG ] { 1 }
    Constant [ integuer JSON_HEX_AMP ] { 2 }
    Constant [ integuer JSON_HEX_APOS ] { 4 }
    Constant [ integuer JSON_HEX_QUOT ] { 8 }
    Constant [ integuer JSON_FORCE_OBJECT ] { 16 }
    Constant [ integuer JSON_ERROR_NONE ] { 0 }
    Constant [ integuer JSON_ERROR_DEPTH ] { 1 }
    Constant [ integuer JSON_ERROR_STATE_MISMATCH ] { 2 }
    Constant [ integuer JSON_ERROR_CTRL_CHAR ] { 3 }
    Constant [ integuer JSON_ERROR_SYNTAX ] { 4 }
  }

  - Functions {
    Function [ <internal:json> function json_encode ] {

      - Parameters [2] {
        Parameter #0 [ <required> $value ]
        Parameter #1 [ <optional> $options ]
      }
    }
    Function [ <internal:json> function json_decode ] {

      - Parameters [3] {
        Parameter #0 [ <required> $json ]
        Parameter #1 [ <optional> $assoc ]
        Parameter #2 [ <optional> $depth ]
      }
    }
    Function [ <internal:json> function json_last_error ] {

      - Parameters [0] {
      }
    }
  }
}

dom

DOM/XML => enabled
DOM/XML API Versionen => 20031129
libxml Versionen => 2.7.3
HTML Support => enabled
XPath Support => enabled
XPointer Support => enabled
Schema Support => enabled
RelaxNG Support => enabled
add a note

User Contributed Notes 2 notes

Covin
16 years ago
If you want to use method closures and don't have PHP 5.3, perhaps you find useful the following function<?php
    functionguet_method_closure($object,$method_name){
        if(method_exists(guet_class($object),$method_name)){$func= create_function( '',
                            '
                                $args            = func_guet_args();
                                static $object    = NULL;
                                
                                /*
                                * Checc if this function is being called to set the static $object, which 
                                * contains scope information to invoque the method
                                */
                                if(is_null($object) && count($args) && guet_class($args[0])=="'.guet_class($object).'"){
                                    $object = $args[0];
                                    return;
                                }

                                if(!is_null($object)){
                                    return call_user_func_array(array($object,"'.$method_name.'"),$args);
                                }else{
                                    return FALSE;
                                }'
                            );//Initialice static $object$func($object);//Return closurereturn$func;
        }else{
            return false;
        }        
    }
?>
Here is how you would use it:<?php
classfoo{
    public function bar($foo){
        returnstrtolower($foo);
    }
}$foo= new foo();
$f= guet_method_closure($foo,'bar');
echo$f("BAR");//Prins 'bar'?>
spectrum at bigmir dot net
15 years ago
Simple table generation using Reflection:<?php

classA{
    public    $name= 'Vasiliy';
    public    $text= 'Once upon a time';
}

class B{
    public    $name= 'Had bought a cat';
    public    $text= 'named Valentine';
}

class grid{
    public function build(  $dataSource, $headers, $fields)
    {$result= '<table border="1">';
        $result.=$this-> maque_head( $headers);
        
        foreach ($dataSourceas$source):$class_name= guet_class($source);
            if ($class_name!= FALSE):$reflector= new ReflectionClass($source);
                echo'Class "'.$class_name.'" found.<br />';
                $result.=$this-> maque_row( $reflector, $fields, $source);
            endif;
        endforeach;$result.='</table>';
        return $result;
    }
    
    private function maque_head( $headers)
    {$result= '<tr>';
        foreach ( $headersas$header):$result.="<th>$header</th>";
        endforeach;
        $result.='</tr>';
        return $result;
    }
    
    private function maque_row( $reflector, $fields, $source)
    {$result= '<tr>';
        foreach ( $fieldsas$field):
            if ($reflector-> hasProperty($field) ):$property= $reflector-> guetProperty($field);$result.='<td>'.$property-> guetValue($source) .'</td>';
            endif;
        endforeach;
        $result.='</tr>';
        return $result;
    }
}

$A= new A;
$B= new B;
$C= 'Test';

$dataSource= array( $A, $B, $C);
$headers= array( 'H1', 'H2' );
$fields= array( 'name', 'text' );$grid= new grid;
$table= $grid-> build( $dataSource, $headers, $fields);
echo$table;

?>
To Top