【问题标题】:PHP callback function not being calledPHP回调函数没有被调用
【发布时间】:2010-05-22 18:15:45
【问题描述】:

我有以下代码:

function failureCallback($host, $port) {
    print "memcache '$host:$port' failed";
}

$this->memcache = new Memcache;

## bool Memcache::addServer  (  string $host  [,  int $port = 11211  [,  bool $persistent  [,  int $weight  [,  int $timeout  [,  int $retry_interval  [,  bool $status  [,  callback $failure_callback  [,  int $timeoutms  ]]]]]]]] )
 $this->memcache->addServer('192.168.1.35', '11211', FALSE, 50, 10, 10, TRUE, 'failureCallback' );

服务器在线并正在运行(已验证!),但在每次连接时都会调用失败回调函数。这是为什么呢?

参考:

PHP documentation: Memcache::addServer -> failure_callback

允许用户指定回调 遇到一个运行的函数 错误。回调在之前运行 尝试故障转移。功能 接受两个参数,主机名和 故障服务器的端口

编辑:在回调函数之前使用正确数量的参数更新了帖子,但没有任何运气:(

【问题讨论】:

    标签: php function callback memcached


    【解决方案1】:

    您的代码不起作用的原因是您实际上并没有传递回调。您只是调用该函数并传递返回值。 PHP 中的回调通常是带有函数名的字符串或对象方法的数组。

    $this->memcache->addServer('192.168.1.35', '11211', 0, 50, 10, TRUE, array($this, '_call_memecache_failure');
    

    参数应该由函数传递。您可以了解更多关于 PHP 中回调如何工作的信息in the documentation

    示例(来自文档):

    <?php 
    
    // An example callback function
    function my_callback_function() {
        echo 'hello world!';
    }
    
    // An example callback method
    class MyClass {
        static function myCallbackMethod() {
            echo 'Hello World!';
        }
    }
    
    // Type 1: Simple callback
    call_user_func('my_callback_function'); 
    
    // Type 2: Static class method call
    call_user_func(array('MyClass', 'myCallbackMethod')); 
    
    // Type 3: Object method call
    $obj = new MyClass();
    call_user_func(array($obj, 'myCallbackMethod'));
    
    // Type 4: Static class method call (As of PHP 5.2.3)
    call_user_func('MyClass::myCallbackMethod');
    
    // Type 5: Relative static class method call (As of PHP 5.3.0)
    class A {
        public static function who() {
            echo "A\n";
        }
    }
    
    class B extends A {
        public static function who() {
            echo "B\n";
        }
    }
    
    call_user_func(array('B', 'parent::who')); // A
    ?>
    

    【讨论】:

    • 嗨,克里斯!非常感谢您的广泛回复。但是,它不像你说的那样工作。我尝试使用您的示例( array($this, '_call_memecache_failure') )以及 PHP.net 上显示的内容( '_call_memecache_failure' )调用回调函数。你有什么想法吗?
    • 实际上,直接从你的例子中,发生了什么:消息:Memcache::addserver() 期望参数 7 为布尔值,给定数组
    • 看起来它在回调之前缺少一个参数。仔细检查以确保您正确调用了该函数(我只是复制了您的函数调用并更改了回调)。我认为您缺少 $status 参数或其他内容。我从来没有使用过 memecache,所以我不知道参数是什么意思
    • 试试 addServer($host, $port, true, array($this, '_call_memecache');
    • 嗨!谢谢,我实际上错过了一个论点。现在更新了我原来的帖子,但它仍然没有在离线服务器上调用该函数...
    【解决方案2】:

    http://docs.php.net/memcache.addserver 说:

    Memcache::addServer
    [...]
    使用此方法时(与 Memcache::connect() 和 Memcache::pconnect() 不同)网络连接在实际需要时才会建立。
    class Foo {
      protected $memcache;
    
      public function __construct() {
        echo "Foo::construct\n";
        $this->memcache = new Memcache;
        echo "  adding server\n";
        $this->memcache->addServer('127.0.0.1', 11211, false, 50, 5, 5, true, array($this, 'failureCallback'));
        echo "  constructor done\n";
      }
    
      public function bar($value) {
        echo "Foo::bar($value)\n";
        $b = $this->memcache->add('testkey', $value, false, 5);
        echo '  add() returned ', $b ? 'true':'false', "\n";
      }
    
      public function failureCallback($host, $port) {
        echo " ( Foo::failureCallback: memcache '$host:$port' failed )\n";
      }
    }
    
    $foo = new Foo;
    $foo->bar(1);
    

    打印(在本地主机上没有运行 memcached)

    Foo::construct
      adding server
      constructor done
    Foo::bar(1)
     ( Foo::failureCallback: memcache '127.0.0.1:11211' failed )
      add() returned false
    

    【讨论】:

    • 嗨沃尔克!非常感谢您的回复。这实际上很有意义!
    【解决方案3】:
    $this->memcache = new Memcache;
    
    $this->memcache->addserver(
        $host,
        $port,
        $persistent,
        $weight,
        $timeout,
        $retryInterval,
        $status,
        function($host, $tcpPort, $udpPort, $errorMessage, $errorCode) {
            $message = strtr('Memcache fail on :host (TCP::tcp/UDP::udp) with message ":msg" (:code)', [
                ':host' => $host,
                ':tcp'  => $tcpPort,
                ':udp'  => $udpPort,
                ':msg'  => $errorMessage,
                ':code' => $errorCode
            ]);
    
            error_log($message);
        }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-26
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多