T. Anre
Новичок
Работа с классами
Вот небольшой класс для работы с классами
Как думаете, что можно доработать?
PHP:
<?php
interface IPHPClass
{
public function __construct($classname);
public function export();
public function newInstance($params = null);
public function invoke($methodname, $params = null);
public function getProperty($propertyname);
public function setProperty($propertyname, $value);
public function getName();
public function getMethodNotation($methodname, $notation = '');
public function getPropertynameNotation($propertyname, $notation = '');
}
class PHPClass implements IPHPClass
{
private $classname = '';
private $Reflection = null;
private $Instance = null;
public function __construct($classname)
{
$this->classname = $classname;
$this->Reflection = new ReflectionClass($this->classname);
}
public function export()
{
echo '<pre>';
ReflectionClass::export($this->classname);
echo '</pre>';
}
public function newInstance($params = null)
{
if (is_null($this->Instance))
$this->Instance = (is_null($params)) ? $this->Reflection->newInstance() : $this->Reflection->newInstance($params);
return $this->Instance;
}
private function createNew($Obj)
{
if (is_null($this->Instance))
if (!$Obj->isPublic() || !$Obj->isStatic())
throw new PHPClassException('Method or Property ' . $this->classname . '::' . $Obj->getName() . ' is not public or static.');
else
if (!$Obj->isPublic())
throw new PHPClassException('Method or Property ' . $this->classname . '->' . $Obj->getName() . ' is not public.');
return $Obj;
}
public function invoke($methodname, $params = null)
{
return $this->createNew(new ReflectionMethod($this->classname, $methodname))->invoke($this->Instance, $params);
}
public function getProperty($propertyname)
{
return $this->createNew(new ReflectionProperty($this->classname, $propertyname))->getValue($this->Instance);
}
public function setProperty($propertyname, $value)
{
$this->createNew(new ReflectionProperty($this->classname, $propertyname))->setValue($this->Instance, $value);
}
public function getName()
{
return $this->classname;
}
private function getNotation($str, $tag = '')
{
if (empty($tag))
return $str;
$matches = array();
preg_match('/@'.$tag.'(.*)(\\r\\n|\\r|\\n)/U', $str, $matches);
if (isset($matches[1]))
return trim($matches[1]);
return '';
}
public function getMethodNotation($methodname, $notation = '')
{
$sDocComment = $this->createNew(new ReflectionMethod($this->classname, $methodname))->getDocComment();
return $this->getNotation($sDocComment, $notation);
}
public function getPropertynameNotation($propertyname, $notation = '')
{
$sDocComment = $this->createNew(new ReflectionProperty($this->classname, $propertyname))->getDocComment();
return $this->getNotation($sDocComment, $notation);
}
}
class PHPClassException extends Exception
{
public function __construct($msg)
{
parent::__construct($msg);
}
}
/*
*
* Пример работы класса PHPClass
*
*/
// Non-static
class A
{
/**
* @author T. Anre
*/
public function sayHello()
{
return 'Non-static, Hello World';
}
}
// Static
class B
{
public static function sayHello()
{
return 'Static, Hello World';
}
}
$A = new PHPClass('A');
$A->newInstance();
echo $A->invoke('sayHello') . "\n<br/>";
echo $A->getMethodNotation('sayHello', 'author') . "\n<br/>";
$B = new PHPClass('B');
echo $B->invoke('sayHello') . "\n<br/>";
// Выведет:
// Non-static, Hello World
// T. Anre
// Static, Hello World
?>

Как думаете, что можно доработать?