Tamam, 1st, ben bu kötü bir davranış olduğunu kabul ediyorsunuz. Ayrıca, 5.3, sen işlevleri (JS tarzı) olarak operatörleri kullanmak için __ çağrı sihirli kelime ile yeni bir kapatma sözdizimi kullanabilirsiniz.
Bu size yol yapmanın bir şekilde tedarik etmek istiyorsanız şimdi, ben __ çağrı büyü ile karışık, create_fnuction kullanarak düşünebilirsiniz.
temelde, uyumlu dizeleri haline dönüştürmek fonksiyonları ve themin Özel üye koymak almak için bir regex desen kullanın. sen onları getirmek için __ çağrı yöntemini kullanmak daha. Ben küçük bir demo üzerinde çalışıyorum.
Tamam, burada sınıftır. i JS tarzı nesneleri uygulamak için kapanışları kullanılan ben bir kaç hafta önce gördüm bir sınıftan ilham aldım:
/**
* 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 <arieh.glazer@gmail.com>
* @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- 1st, the source cannot have any php tags. 2nd, functions will always be public. 3rd- we can only mimic $this. what i did was to pass as a 1st argument $this, and the second is the array of arguments (which is a 4th limition). also, you will not be able to access non-public members and methods from within the class.
an example for a source file:
function a($self,$arr=array()){
//assuming the object has a member called str
echo $self->str;
}
Bu benim için eğlenceli bir egzersiz, ama tüm kötü bir uygulama olduğunu