Получение информации о методах и свойствах php класса через Reflection
Тесты для Reflection в php
26.06.2017
Не вижу смысла расписывать все детально, просто оставлю это здесь, для упрощения новых тестов:
index.php (Download)
<?
require_once('in.php');
$sClass = 'myTest';
$class = new ReflectionClass($sClass);
echo 'Class:<br />';
echo '<pre>';
print_r($class);
echo '</pre><hr />';
$sMethod = 'pub1';
$method = $class->getMethod($sMethod);
echo 'Method:<br />';
echo '<pre>';
print_r($method);
echo '</pre><hr />';
$param = $method->getParameters();
echo 'Param:<br />';
echo '<pre>';
print_r($param);
echo '</pre><hr />';
$doc = $method->getDocComment();
echo 'Doc:<br />';
echo '<pre>';
print_r($doc);
echo '</pre><hr />';
$constructor = $class->getConstructor();
echo '$constructor:<br />';
echo '<pre>';
print_r($constructor);
echo '</pre><hr />';
function myfunction($a, $b) { return $a*$b; }
//$func = new ReflectionFunction('myfunction');
$func = new ReflectionClass($sClass);
$filename = $func->getFileName();
$start_line = $func->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block
$end_line = $func->getEndLine();
$length = $end_line - $start_line;
$source = file($filename);
$body = implode("", array_slice($source, $start_line, $length));
print_r($body);
in.php (Download)
<?
/**
* Тестовый класс
*/
class myTest
{
protected $_proto1 = NULL;
protected $_proto2 = '0';
protected $_proto3 = 0;
public $sText = '';
/**
* Конструктор
*
* @return
*/
function __construct()
{
}
/**
*
* @param int $x
* @param int $y
*
* @return
*/
public function pub1($x, $y)
{
return $x * $y;
}
public function pub2()
{
echo 'pub2 <br />';
}
/**
* proto
*
* @return
*/
protected function pro1()
{
echo 'pro1 <br />';
}
protected function _pro1()
{
echo '_pro1 <br />';
}
private function _priv1()
{
echo '_priv1 <br />';
}
static public function stztic1($x, $y)
{
return $x * $y;
}
}