hey all i want to use ReflectionClass to load some classes defined in my xml file
but i am getting an error
Error while register class :Class ReportErrorHandler does not exist
i will not get this error when i will use
require_once( 'handlers/system/ReportErrorHandler.php' );
but i dont want to require_once the class ,this is why i am using reflection and in my xml file i am writing only the class name with no full path ,so i cant dynamically 'require_once'
here is my code
public function init(){
$doc = new DOMDocument();
$doc->load( 'server.xml' );
$handlers = $doc->getElementsByTagName( "handler" );
foreach( $handlers as $handler ) {
$this->register($handler->nodeValue);
}
}
public function register(&$name) {
try{
$intercafe = new ReflectionClass('Handler');
$reflectionClass = new ReflectionClass($name);
if(!$reflectionClass->isSubclassOf($intercafe)){
//LOG ERROR
error_log("Init-> Error ," + name + " is not a subclass of [net/Handler]");
throw new InvalidArgumentException();
}
error_log("register: " . $reflectionClass);
$handler = $reflectionClass->newInstance();
$type = $handler->getType();
//LOG DEBUG
syslog(LOG_DEBUG ,"Registering handler = " . $name . " TYPE = " . $type);
$key = $type << 32;
$this->table[$key] = $reflectionClass;
}catch(Exception $ee){
error_log("Error while register class :" . $ee->getMessage());
}
}
my xml file
<?xml version="1.0" encoding="UTF-8"?>
<server>
<server-version>0.1</server-version>
<handlers>
<handler>AuthenticateHandler</handler>
<handler>ReportErrorHandler</handler>
<handler>DisconnectedHandler</handler>
<handler>NoSuchRequestHandler</handler>
</handlers>
</server>
but i dont want to require_once the class ,this is why i am using reflection
Even reflection needs to know what it is that it's supposed to be reflecting. Why don't you want to include/require the class definition?
Setup Autoloading with
spl_autoload_register
— Register given function as __autoload()
implementation this is what is did to solve my problem i separate the full path for require_once
public function register(&$name) {
try{
$intercafe = new ReflectionClass('Handler');
$fullPath = $name;
$className = $name;
$index = strrpos($name , "/");
if($index != False){
$className = substr($name ,$index + 1);
}
$requirePath = 'handlers/' . $fullPath . '.php';
require_once ( $requirePath );
$reflectionClass = new ReflectionClass($className);
if(!$reflectionClass->isSubclassOf($intercafe)){
//LOG ERROR
error_log("Init-> Error ," + name + " is not a subclass of [net/Handler]");
throw new InvalidArgumentException();
}
$handler = $reflectionClass->newInstance();
$type = $handler->getType();
//LOG DEBUG
syslog(LOG_DEBUG ,"Registering handler = " . $name . " TYPE = " . $type);
$key = $type << 32;
$this->table[$key] = $reflectionClass;
}catch(Exception $ee){
error_log("Error while register class :" . $ee->getMessage());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.