Passing an Instance Method as Argument in PHP

Passing an instance method as argument in PHP

  • Before PHP 5.4, there was no type named callable, so if you use it as a type hint, it means "the class named callable". If you use PHP >= 5.4, callable is a valid hint.

  • A callable is specified by a string describing the name of the callable (a function name or a class method name for example) or an array where the first element is an instance of an object and the second element is the name of the method to be called.

For PHP < 5.4, replace

public function add(callable $function)

with:

public function add($function)

Call it with:

$listener->add(array($this, 'bar'));

Passing method as parameter in PHP

You need to describe the RandomNumberStore::store_number method of the current instance as a callable. The manual page says to do that as follows:

A method of an instantiated object is passed as an array containing an
object at index 0 and the method name at index 1.

So what you would write is:

generate_number([$this, 'store_number']);

As an aside, you could also do the same in another manner which is worse from a technical perspective, but more intuitive:

generate_number(function($int) { $this->store_number($int); });

Accept function as parameter in PHP

It's possible if you are using PHP 5.3.0 or higher.

See Anonymous Functions in the manual.

In your case, you would define exampleMethod like this:

function exampleMethod($anonFunc) {
//execute anonymous function
$anonFunc();
}

Pass instance of model in method parameter

Branch::class is a class constant that returns the class name as a string, as the error message suggests. Perhaps you're looking to pass new Branch instead.

Pass method with argument to other class method

http://docs.php.net/manual/en/language.types.callable.php says:

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.
<?php
class Foo /* extends Database */ {
private $crumb = 'Hello';

public function breadcrumb( callable $translate ) {
return $translate($this->crumb);
}
}

class Bar /* extends Database */ {
private $translation = ['Hello'=>'Hallo'];

public function translate($word) {
return $this->translation[$word];
}
}

$foo = new Foo;
$bar = new Bar;
echo $foo->breadcrumb( [$bar, 'translate'] ); ?>

(you also forgot the $this-> reference for accessing the instance memeber translation.)

Passing a class as function parameter

You can use the magic ::class constant:

public function uniqueSlug($str, $model)
{
$slug = Str::slug($str);

$count = $model::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();

return $count ? "{$slug}-{$count}" : $slug;
}

$newcat->slug = $helper->uniqueSlug($appname, Apk::class);

Passing static methods as arguments in PHP

The 'php way' to do this, is to use the exact same syntax used by is_callable and call_user_func.

This means that your method is 'neutral' to being

  • A standard function name
  • A static class method
  • An instance method
  • A closure

In the case of static methods, this means you should pass it as:

myFunction( [ 'MyClass', 'staticMethod'] );

or if you are not running PHP 5.4 yet:

myFunction( array( 'MyClass', 'staticMethod') );

Passing a class, method and arguments to WordPress functions

You should be able to pass an anonymous function instead, whose body would simply call the bar method with the appropriate arguments.

The anonymous function would look like this:

function () { $this->bar('Bob'); }

Or alternatively, if you're using PHP 7.4+:

fn() => $this->bar('Bob')

So, just pass that as the callback, like this:

add_menu_page(
$page_title,
$menu_title,
$capability,
$menu_slug,
// fn() => $this->bar('Bob'),
function () {
$this->bar('Bob');
},
$icon_url,
$position
);

Note: I'm very unfamiliar with WordPress so this might not be the most appropriate way to do this.



Related Topics



Leave a reply



Submit