Static function of an access class through a variable

So, I have a PHP class called a router that takes a URL and blows it up to find the requested component, action, and any given values. Then it loads the responsible class, starts the action, etc. Etc. Now I integrate user access to the user through the user class.

For each component (which is a class), I have a class variable of a static class called "perms" that contains each action as an index and a number as the minimum resolution needed to trigger the action. Each component also has a static function to get the permission value for the name of the passed action.

The problem I ran into is getting the static function to work correctly with the name of the class stored in the variable. In the router, I use a variable to store the name of the component.

$this->controller // cms, calendar ,etc

Then I add it "Controller" to get the class name

$class = $this->controller.'Controller'; // cmsController, calendarController, etc

However, I get an error when I try to use it to access a static function

$minActionPerm = $class::getPerms( $this->action ); // No go, parse error

I don't get errors when I literally print the class name, but this is not a real solution.

$minActionPerm = cmsController::getPerms( $this->action ); // Good, but literal

This variable also works when I create a class object to trigger an action.

$object = new $class();

I am sure this is probably the simple answer - for example, using a variable, but I don't know how it is now.

+3
source share
1 answer

You need to use the function call_user_func.

Example:

call_user_func(array($class, 'getPerms'), $this->action);

PHP 5.2.3+, :

call_user_func($class . '::getPerms', $this->action);
+5

Source: https://habr.com/ru/post/1716742/


All Articles