Показать сообщение отдельно
Старый 27.09.2013, 10:16   #3
Черный крыс
 
Сообщений: n/a
Ответ: Странное поведение массива...

Не знал про такую особенность.

работающий код ->

<?php

class Hook {
    
    protected 
$_data = null;
    
    public function 
add($func, $context=null, $priority=0) {
        
        if (
is_callable($func) == false) { return false; }
        
        
$t = [$func, $context, $priority, null];
        
        
$pred = null;
        
$hook = &$this->_data;
        
        while (
$hook != null) {
            if (
$priority > $hook[2]) { break; }
            
$pred = &$hook;
            
$hook = &$hook[3];
        }
        
        if (
$pred != null) {
            
$t[3] = &$pred[3];
            
$pred[3] = &$t;
        } else {
            
$t[3] = &$this->_data;
            
$this->_data = &$t;
        }
        
        return 
true;
    }
    
    public function 
run($data=null) {
        
$hook = &$this->_data;
        while (
$hook != null) {
            
$data = call_user_func($hook[0], $data, $hook[1]);
            
$hook = &$hook[3];
        }
        return 
$data;
    }
    
    public function 
rem($func, $context=null) {
        
$pred = null;
        
$hook = &$this->_data;
        
        while ((
$hook != null) and (($hook[0] != $func) or ($hook[1] != $context))) {
            
$pred = &$hook;
            
$hook = &$hook[3];
        }
        
        if (
$hook == null) { return false; }
        
        if (
$pred != null) {
            
$pred[3] = &$hook[3];
        } else {
            
$this->_data = &$hook[3];
        }
        
        return 
true;
    }

};

/* ***EXAMPLE*** */

function test1($data, $context) {
    return 
$data.' Hello!';
}

function 
test2($data, $context) {
    return 
$data.' How are you?';
}

$hook = new Hook;

$hook->add('test1');
$hook->add('test2');
echo(
$hook->run('Albert'));
$hook = null;



?>
 
Ответить с цитированием