PHP Static Method Call with Variable Class Name and Namespaces

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);

PHP static method call with namespace in member variable

Yes something like this is possible. There is is static class method which can be called which returns the namespace path of the class.

<?php
namespace Stripe;

Class Stripe {

public static function setApiKey($key){
return $key;
}

}

class StripeLib
{
public $stripe;

public function __construct()
{
// Put the namespace in a member variable
$this->stripe = '\\'.Stripe::class;
}
}

$s = (new StripeLib())->stripe;

// Call the static setApiKey method of the Stripe class in the Stripe namespace
echo $s::setApiKey("testKey"); //Returns testkey

PHP call a static method of a class by class name

Check out this post. How can I call a static method on a variable class?

It look like your code is ok in php 5.3. There's also some ideas how to deal with your problem if you are < 5.3.

Calling a static method when the class is a variable with namespaces

I found this question much sooner than I found a resolution to it. For everyone's benefit, here is a helpful link to resolving the issue at hand: Variable functions with namespaces in PHP

Call static method from variable class

This is a namespace issue. When using variables for class names, you need to provide the full class name including namespace.

Assuming all your Cliente, Fornecedor, Grupo, etc classes are in the same namespace (probably AGC\Bundle\AGCPedidosBundle\Entity), you can probably get away with something like this...

private function atualizaDB($arquivo, $entidade) {
$entidade = '\\AGC\\Bundle\\AGCPedidosBundle\\Entity\\'.$entidade;

// and the rest...
}

How to reference a static class variable inside a namespace?

Since your code works when it's in the global namespace, but fails when it's in a custom namespace, you'll need to provide a fully-qualified class name whenever you use external classes. So in this situation, simply change your code to:

public static function init() {
self::$request = new \Something();
}

And if for some reason this doesn't solve your issue, then you can at least rule out static variables as being related to the problem by (temporarily) changing self::$request = to $temp = and observing that the problem still occurs.

error when using variable class name and static method

can anyone think of a reason why the class name isn't resolving correctly when using the first example versus the second?

The PHP parser does not support such a syntax, and that's merely all. This is because the parser has grown historically. I can't give more reason than that.

It will be that with PHP 7 you can see some changes on these syntax details working more into your expected direction Uniform Variable Syntax:

($variable->other['class'])::my_static_method();

But until then, you can go around that with the help of call_user_func:

call_user_func([$variable->other['class'], 'my_static_method']);
call_user_func($variable->other['class'] . '::my_static_method');

Or as you wrote your own, by creating a variable:

$class = $variable->other['class'];
$class::my_static_method();

Or even a variable that looks like something different:

${(int)!${0}=$variable->other['class']}::my_static_method();

Related Material:

  • Interpolation (double quoted string) of Associative Arrays in PHP

PHP: Static method in Class vs Global function in Namespace?

Is there any reason to prefer one method over the other?

(below assumes you're asking/referring to a difference between a static class method and a function in a namespace)

Historically (before namespaces arrived), people were forced to use classes with static functions to not pollute a global namespace. This is one of the reasons you're likely to encounter this method more often.

You might prefer to use a static method if you need access to class's data or if you think of leaving a door to overriding them in descendant classes (the so-called 'flexibility and functionality' by some).

You might prefer a namespaced function when all you want is ...uh, ahem... a function. Think of some util, functional or collections functions like compose/partial, retry , filter/map/some/every etc. You are probably not expecting this to be overwritten in a child class, right? Neither it need access to some static class member.

There is one particular annoying problem with (namespaced) functions though, that classes don't suffer from. And it's autoloading. To put it short, there is no way to autoload a function at the moment. And this is another reason you'd likely encounter classes with static methods (when they don't necessarily need to be) more often in practice.

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));

Php call static method of class which is in variable

You can use call_user_func for this.

call_user_func($name.'::methodName');

Or:

call_user_func(array($name, 'methodName'));


Related Topics



Leave a reply



Submit