Dynamic Static Method Call in PHP

Dynamic static method call in PHP?

Use the call_user_func function:

http://php.net/manual/en/function.call-user-func.php

Example:

call_user_func('myClassName_' . $language . '::myFunctionName');

PHP static method call with variable class name and namespaces

When using a variable to reference your class, you need to use a fully qualified name. Try this...

$type = __NAMESPACE__ . '\\' . $product->type;
$product = $type::find($product->id);

How do I dynamically invoke a class method in PHP?

It works both ways - you need to use the right syntax

//  Non static call
call_user_func( array( $obj, 'method' ) );

// Static calls
call_user_func( array( 'ClassName', 'method' ) );
call_user_func( 'ClassName::method' ); // (As of PHP 5.2.3)

PHP: Call Class' Static Method with Dynamic Class Name pre PHP 5.3

You can upgrade your PHP version. Since 5.3 you can use they way you want to call your function.

There is no other way in 5.2.

You can use this writing, maybe more easy to read :

$stuff = call_user_func_array(array($class, $method), array($arg1, $arg2));

How does Laravel dynamically call a class method statically without implicitly using the static keyword for the method in PHP?

My guess here is that your User class actually extends the Laravel Model class.

This class implements some of PHPs so called magic methods. You can find our about them here:
https://www.php.net/manual/en/language.oop5.magic.php

One of these is __callStatic.

In Model.php:

/**
* Handle dynamic static method calls into the method.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}

Allow PHP function to be called dynamically and statically

Just out of pure interest I've found a way to do this using __call and __callStatic and setting the methods you want to call to be inaccessible to the scope that is calling it (i.e. set the methods as private or protected).

Important warning: Using __call or __callStatic can lead to unexpected behavior! First read here to make sure you understand this. The code below dynamically executes the private class methods called from outside the class, so you need to be sure to whitelist specific methods that you want to add this behavior to. There may be other security issues that I have not dealt with, so use at your own risk.

class MethodTest
{

private static $obj;
private $fruit = 'watermelon';

private function handleDynamicCallType($name, $arguments)
{
switch ($name) {
case 'method1':
case 'method2':
$this->{$name}($arguments);
break;
default:
throw new Exception("Invalid name");
}
}

private function method1() {
echo $this->fruit . ' ';
}

private function method2() {
$arg_list = func_get_args();
if (!empty($arg_list[0][0])) {
$this->fruit = $arg_list[0][0];
}
}

public function __call($name, $arguments)
{
$this->handleDynamicCallType($name, $arguments);
return $this;
}

public static function __callStatic($name, $arguments)
{
if (!isset(self::$obj)) {
self::getNewStaticInstance();
}
self::$obj->{$name}($arguments);
return self::$obj;
}

public static function getNewStaticInstance()
{
self::$obj = new MethodTest();
return self::$obj;
}
}

$obj = new MethodTest;
$obj->method1()::method2('orange')->method1();
echo PHP_EOL;
MethodTest::method1()->method2('plum')::method1();

Output:

watermelon orange 
orange plum

However the static object retains its properties after being called previously (notice the two orange strings). If this is undesired, we can force it to reset by calling getNewStaticInstance():

MethodTest::method1()::method2('plum')::method1()::getNewStaticInstance()::method1();

Output:

watermelon plum watermelon 

PHP Calling a function within a static method of a class

Move the function deceleration to before you attempt to use it when defining functions within other functions...

class MainClass
{
public static function myStaticMethod()
{
function myFunction()
{
echo 'hello';
}
return myFunction();
}
}

MainClass::myStaticMethod(); // No error thrown

Note that repeat calls to MainClass::myStaticMethod will raise Cannot redeclare myFunction() unless you manage that.

Otherwise, move it outside of your class

function myFunction()
{
echo 'hello';
}

class MainClass
{
public static function myStaticMethod()
{
return myFunction();
}
}


Related Topics



Leave a reply



Submit