Skip to content

Tag: callback

Add a callback closure to a class constructor

I have a class that someone else wrote. It does a lot of things for me, and one thing it does for me is it makes requests to an external service. So what I wanted to do is modify the constructor of the class so that I could pass in a function, and the function could get called after the

add_submenu_page in WordPress

When using the add_submenu_page to create a new submenu in WooCommerce, I get “Cannot modify header information – headers already sent”. The add_submenu_page is as follows: Changing that to the following removes the “Cannot modify header information – headers already sent” …

Call object member function on all array members

This may be a bit of a silly question and its one of convenience rather than functionality. Assume we have the class: Let’s say we have an array of objects of type A, e.g. $array = [ new A(), new A(), new A() ]; Normally if I want to transform the array of all objects into an array of their

How to use class methods as callbacks

I have a class with methods that I want to use as callbacks. How can I pass them as arguments? Answer Check the callable manual to see all the different ways to pass a function as a callback. I copied that manual here and added some examples of each approach based on your scenario. Callable A PHP function is …

call methods and objects using php

i have this code : in my code i don’t always want to use $test = new test …. I want to use the name directly instead of $test. For example: Answer You should use a factory method pattern or something like that. With a factory method you can create a class based on a string. In PHP it’s not

pass a callback WITH arguments in PHP

I have a PHP library function expecting a callback with no arguments. I know I can pass an object’s method with array($this , ‘my_function_name’) but how can I give parameters to the my_function_name? I have found a solution using create_function but from PHP manual I see it has security iss…