note 115412 added to reflectionmethod.invokeargs
| From: | msajadi824 at hotmail dot com | Date: | Sun, 20 Jul 2014 06:52:20 +0000 |
| Subject: | note 115412 added to reflectionmethod.invokeargs | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199981@lists.php.net to get a copy of this message | ||
A function to run a private or protected functions from a class:
<?php
function privateToPublic($class, $function, array $argumentForFunction = array())
{
$method = new \ReflectionMethod($class, $function);
$method->setAccessible(TRUE);
return $method->invokeArgs($class,$argumentForFunction);
}
?>
example:
<?php
class a {
private function test($a, $b) {
return $a + $b;
}
}
function privateToPublic($class, $function, array $argumentForFunction = array())
{
$method = new \ReflectionMethod($class, $function);
$method->setAccessible(TRUE);
return $method->invokeArgs($class,$argumentForFunction);
}
echo privateToPublic(new a(), 'test', array(10,20));
?>
result:
30
----
Server IP: 72.52.91.14
Probable Submitter: 87.107.179.69
----
Manual Page -- http://php.net/manual/en/reflectionmethod.invokeargs.php
Edit -- https://master.php.net/note/edit/115412
Del: integrated -- https://master.php.net/note/delete/115412/integrated
Del: useless -- https://master.php.net/note/delete/115412/useless
Del: bad code -- https://master.php.net/note/delete/115412/bad+code
Del: spam -- https://master.php.net/note/delete/115412/spam
Del: non-english -- https://master.php.net/note/delete/115412/non-english
Del: in docs -- https://master.php.net/note/delete/115412/in+docs
Del: other reasons-- https://master.php.net/note/delete/115412
Reject -- https://master.php.net/note/reject/115412
Search -- https://master.php.net/manage/user-notes.php