Skip to content

Instantly share code, notes, and snippets.

@AgelxNash
Created June 4, 2013 11:34
Show Gist options
  • Save AgelxNash/5705282 to your computer and use it in GitHub Desktop.
Save AgelxNash/5705282 to your computer and use it in GitHub Desktop.
<?php
trait HiddenCaller{
public function __call($key, $params){
try{
if(self::_checkMethod($key)){
self::_getMethod($this,$key,$params);
}
}catch(Exception $e){die($e->getMessage());}
}
public static function __callStatic($key, $params){
try{
if(self::_checkMethod($key)){
self::_getMethod(A,$key,$params);
}
}catch(Exception $e){die($e->getMessage());}
}
private static function _checkMethod($key){
$flag = false;
if(method_exists(__CLASS__,$key) || method_exists(get_parent_class(),$key)){
$flag = true;
}else throw new Exception('unknown method '.$key);
return $flag;
}
private static function _getMethod($obj,$key,$params){
$flag = null;
$method = new ReflectionMethod($obj,$key);
if($method->isPrivate() || $method->isProtected()){
$method->setAccessible(true); //PHP 5.3.2
if(is_string($obj) && $method->isStatic()){
$obj = new $obj;
}
$flag = $method->invokeArgs($obj,$params);
unset($obj);
}else throw new Exception('is no private '.$key);
return $flag;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment