Ejemplo n.º 1
0
<?php

class test
{
    public $member;
    function test()
    {
        $this->member = 1;
        register_shutdown_function(array($this, 'destructor'));
    }
    function destructor()
    {
        print __METHOD__ . "\n";
    }
    function __destruct()
    {
        print __METHOD__ . "\n";
    }
    function add()
    {
        $this->member += 1;
        print $this->member . "\n";
    }
}
$t = new test();
$t->add();
$t->add();
echo "Done\n";
Ejemplo n.º 2
0
<?php

class test
{
    public function add($num1, $num2, $callback)
    {
        $sum = $num1 + $num2;
        $callback($sum);
    }
    public function call_from_string($num1, $num2, $callbackString)
    {
        $sum = $num1 + $num2;
        call_user_func($callbackString, array($num1, $num2, $sum));
    }
}
function display_string($params)
{
    foreach ($params as $key => $param) {
        echo '<p>Params: ' . $key . ' = ' . $param;
    }
}
$test = new test();
// pass literal callback function
$test->add(1, 1, function ($result) {
    echo $result;
});
// pass callback via string
$test->call_from_string(2, 3, 'display_string');