오케이, 1 번, 나는 이것이 나쁜 매너라는 데 동의합니다. 또한 5.3에서 연산자를 함수 (JS 스타일)로 사용하기 위해 __call 매직 워드와 함께 새로운 클로저 구문을 사용할 수 있습니다.
지금이 방법을 제공하려면 create_fnuction을 __call 매직과 혼합하여 사용하는 것이 좋습니다.
기본적으로 정규식 패턴을 사용하여 함수를 호환 가능한 문자열로 변환하고 themin을 비공개 멤버로 설정합니다. __call 메서드를 사용하여 가져 오는 것보다 낫습니다. 나는 작은 데모를 만들고있다.
괜찮습니다. 다음은 클래스입니다.
/**
* supplies an interface with which you can load external functions into an existing object
*
* the functions supplied to this class will recive the classes referance as a first argument, and as
* a second argument they will recive an array of supplied arguments.
*
* @author arieh glazer <[email protected]>
* @license MIT like
*/
class Function_Loader{
/**
* @param array holder of genarated functions
* @access protected
*/
protected $_funcs = array();
/**
* loads functions for an external file into the object
*
* a note- the file must not contain php tags.
*
* @param string $source a file's loaction
*
* @access public
*/
public function load($source){
$ptrn = '/function[\s]+([a-zA-Z0-9_-]*)[\s]*\((.*)\)[\s]*{([\w\s\D]+)}[\s]*/iU';
$source = file_get_contents($source);
preg_match_all($ptrn,$source,$matches);
$names = $matches[1];
$vars = $matches[2];
$funcs = $matches[3];
for ($i=0,$l=count($names);$i<$l;$i++){
$this->_funcs[$names[$i]] = create_function($vars[$i],$funcs[$i]);
}
}
public function __call($name,$args){
if (isset($this->_funcs[$name])) $this->_funcs[$name]($this,$args);
else throw new Exception("No Such Method $name");
}
}
limitations- 1 일, 소스가 어떤 PHP 태그를 가질 수 없습니다 : 내가 JS 스타일의 객체를 구현하는 클로저를 사용 내가 몇 주 전에 본 클래스에서 영감을 얻었다. 둘째, 기능은 항상 공개됩니다. 셋째, 우리는 $ this를 모방 할 수 있습니다. 내가 한 일은 1 인자 $ this로 전달하는 것이고, 두 번째 인자는 인자 배열 (4 번째 제한)이다. 또한 수업 내에서 비공개 멤버 및 메서드에 액세스 할 수 없습니다. 소스 파일에 대한 예 :
function a($self,$arr=array()){
//assuming the object has a member called str
echo $self->str;
}
이 나에게 재미있는 운동을했지만, 모든
$ registry는 전역 액세스를위한 함수 및 변수를 저장하는 클래스입니다. – saint