V
V
vostotskiy2017-08-23 12:48:20
C++ / C#
vostotskiy, 2017-08-23 12:48:20

Is it possible to call a private method inside an anonymous function?

Hello. There is some Klass class with several methods, which checks some arguments using an anonymous function.

class Klass{
public function check(array $data, callable $function)
    {
        if (!$function($this->testAmount)) {
            throw new \Method exceptionException("Something went wrong");
        }
    }
 private function test($value1, $value2) {
        return $value1== $value2;
    }
}

In another class, we call this class and implement a callback with a call to the test method to check the equality of variables.
$class = new Klass;
            $class->check($data, function($someValue) use($amount){
                return $this->test($amount, $someValue);
});

But the problem is that there is no access to the private method of the Klass class in the callback and an error occurs. How to implement a callback or pass parameters to it in such a way that you can call a private method of the Klass class inside?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir S, 2019-04-27
@ase2015

add the PurchaseManager_OnPurchaseNonConsumable method to the game class

E
Eugene Mosyukov, 2017-08-23
@GeneMoss

You need to bind your closure to the desired object:

$class = new Klass;
$closure = function($someValue) use($amount) {
    return $this->test($amount, $someValue);
};
$closure = $closure->bindTo($class, 'Klass');
$class->check($data, $closure);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question