Другие ответы пока хороши, но, увидев, что ваш основной интерес к событиям, я покажу вам, как это сделать, вместе с цепочкой (бонус!).
/**
* jQuery events+chaining prototype.
*/
class pQuery {
/// PROTECTED PROPERTIES ///
/**
* @var array Holds list of event=>callbacks items.
*/
protected $events=array();
/// PROTECTED UTILITY METHODS ///
/**
* Creates an anonymous function compatible with PHP 4+.
* <b>IMPORTANT</b> It is likely functions made this way are not garbage collected.
* @param $funcCode string Function decleration, including the body.
* @return string The newly created function's name.
*/
protected static function mk_anonFunc($funcCode){
static $counter=0;
$func='pQanon'.(++$counter);
@eval('function '.$func.'('.substr($funcCode,9));
if(!function_exists($func))
die('Fatal: Anonymous function creation failed!');
return $func;
}
/**
* Detects whether a string contains an anonymous function or not.
* @param $funcCode string Function decleration, including the body.
* @return boolean True if it does contain an anonymous function, false otherwise.
*/
protected static function is_anonFunc($funcCode){
return strpos($funcCode,'function(')!==false;
}
/// PUBLIC METHODS ///
/**
* Bind event callback to this jQuery instance.
* @param string $event The event name, eg: 'click'.
* @param string|array $callback A function or a class/instance method, eg:
* 'myFunc' OR array('myClass','myMtd') OR array($obj,'myMtd').
* @return pQuery Chaining.
*/
public function bind($event,$callback){
if(self::is_anonFunc($callback))
$callback=self::mk_anonFunc($callback);
if(!isset($this->events[$event]))
$this->events[$event]=array();
$this->events[$event][]=$callback;
return $this;
}
/**
* Unbind event callback from this jQuery instance.
* @param string $event The event name, eg: 'click'.
* @param string|array $callback A function or a class/instance method, eg:
* 'myFunc' OR array('myClass','myMtd') OR array($obj,'myMtd').
* @return pQuery Chaining.
*/
public function unbind($event,$callback){
if(!isset($this->events[$event])){
if(($pos=array_search($callback,$this->events[$event]))!==false)
unset($this->events[$event][$pos]);
}
return $this;
}
/**
* Trigger event, calling all related callbacks.
* @param string $event The event name to trigger, eg: 'click'.
* @param array $params Optional array of arguments to pass to callback.
* @return pQuery Chaining.
*/
public function trigger($event,$params=array()){
if(isset($this->events[$event]))
foreach($this->events[$event] as $callback)
call_user_func_array($callback,$params);
return $this;
}
}
/**
* Allows us to use factory-singleton pattern.
* @return pQuery The $pQuery instance.
*/
function pQuery(){
global $pQuery;
if(!isset($pQuery))
$pQuery=new pQuery();
return $pQuery;
}
И вот пример:
// instantiate pQuery
// Note: to use it like jQuery does, do it as follows
// and then simply use "global $pQuery;" wherever you want it.
$pQuery = new pQuery();
// declare some sample callbacks
function start(){ echo 'start'; }
function stop(){ echo 'stop'; }
// bind some stuff and call start
$pQuery->bind('start','start')
->bind('stop','stop')
->trigger('start');
// bind more stuff and call click
$pQuery->bind('click','function(){ echo "clicked"; }')
->bind('custom','function(){ echo "custom"; }')
->bind('click','function(){ echo "clicked2"; }')
->trigger('click',array($pQuery,'arg2','arg3'));
// call end
$pQuery->trigger('stop');