php: reflection bidez metodo dinamikuek erabili

php-ntzako reflection API-dxe erabilitze, al’tzagu dinamismue emon geure programari, adibidez metodo dinamikueri deitsu, jakin barik ze motatako metodue dan, adibidez:

<?php
$class = 'a';
$method = 'b';
$params = array('d' => 'e');

// klasie existiten badan
if(class_exists($class)) 
{
        // metodue existiten badan
        if(method_exists($class, $method))
        {
                // reflection objetue sortu
                $reflection = new ReflectionMethod($class, $method);
                // metodue estatikue badan
                if($reflection->isStatic())
                {
                        $f = $class."::".$method;
                        $result = call_user_func($f, $params);
                }
                // metodue publikue badan leleu klasien instantzidxe sortu bi'de
                elseif($reflection->isPublic())
                {
                        $instance = new $class();
                        $result = $instance->$method($params);
                }
        }
}
if(isset($result))
{
        echo "<pre>";
        print_r($result);
        echo "</pre>";
}

class a
{
        public static function b($params = NULL)
        {
                return array('b' => $params);
        }
        public function c($params = NULL)
        {
                return array('c' => $params);
        }
}
?>

Erantzune:
<pre>Array                                                 
(                                                        
    [b] => Array                                                                                                                                                                   
        (                                                                                                                                                                          
            [d] => e                                                                                                      
        )                                                        
)                                                        
</pre>

Oin metodue ‘c’ imindxe:

$method = 'c';

Erantzune:

<pre>Array                                                 
(                                                        
    [c] => Array                                                                                                                                                                   
        (                                                                                                                                                                          
            [d] => e                                                                                                      
        )                                                        
)                                                        
</pre>

Gauze gidxau al diez egin, horretarako leidu RELECTION PHP API-dxe :)

Leave a Reply

Your email address will not be published. Required fields are marked *