0

I received the following error :

Closure Object ( [this] => test1 Object ( ) [parameter] => Array ( [$handler] => ) )

I have no idea what it means, I am trying out new things with php. My purpose is to call an anonymous function to execute a list of commands on the fly, for the purpose of learning php call backs and closures i created 3 classes.

test1, test2 extends hand

<?php

// test1.php

class test1 
{
    public function __construct() {
    }    

    function dataTable() {
        test2::table("food", function(hand $handler){
            $handler->put("cookies");
        });
    }

}

// test2.php

class test2 extends hand
{
    public function __construct() {
        echo $this->info;
    }

    static function table($s, $b) {
        echo "called from Test2, Table 1st parameter ";
        echo $s ." <br><br> &nbsp";
        echo "called from Tes2, table 2nd parameter ";
        print_r($b);
        echo "---end";
    }
}

// hand.php

class hand
{
    protected $info;

    public function __construct() {
        print_r($this->info);
    }

    function put($b) {
        $this->info = $b;
    }
}

I want to retrieve the result entered here $handler->put("cookies");

from within the hand class and test2 class, the first parameter works as expected but i received an unfamiliar php error with the second parameter.

Please help, what am i doing wrong or aren't doing? i am trying to understand the callback and closures

1
  • Hi Darragh, thanks abunch, it was quite helpful, i have now passed in an argument of type hand but returns null, any suggestions? Commented May 28, 2015 at 14:25

1 Answer 1

1

Okay, I am going to extend my comment into an answer.

First of all, you are not receiving an error. It is the output from the line print_r($b);.

You have successfully passed the anonymous function to test2::table() as the second argument $b. However you have not executed it. To do so you need to call it. You execute an anonymous function (with no arguments) like so:

$b();

However, in your code $b requires a single argument of type hand, otherwise you will receive a Catchable fatal error and your code will fail at that point.

Since the test2 class extends from hand I am guessing that you intend to call the anonymous function with the current object context, like so:

$b($this);

However this will not work, because the test2::table() method is static. Therefore, it does not have specific object context, which means $this is not available.

The following is a simplified example of what I think you are trying to achieve more or less:

<?php

class Hand
{
    protected $info;

    function put($b)
    {
        $this->info = $b;
    }
}

class Test2 extends Hand
{
    function table($s, $b)
    {
        var_dump($this->info); // outputs null
        $b($this);             // execute the anonymous function
        var_dump($this->info); // outputs 'cookies'
    }
}

class Test1
{
    function dataTable()
    {
        $test2 = new Test2();
        $test2->table('food', function(Hand $handler){
            $handler->put('cookies');
        });
    }
}

$test1 = new Test1();
$test1->dataTable();

Note, that the table method is no longer static. Now you can pass it the executing object's context. Since all the code does is assign the value "cookies" to property Hand::$info; I've also added some var_dump calls to show that the property is null before executing the anonymous function and set to 'cookies' afterwards.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.