TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Variable class names (http://www.talkphp.com/advanced-php-programming/2983-variable-class-names.html)

delayedinsanity 06-20-2008 06:55 PM

Variable class names
 
I'm building a module system into something I'm coding right now, and in determining the best way to build a menu based off what modules/plugins are available I came across something I'm not sure if I can do or not.

Right now I have a very simple stopgap in that uses GET information to determine what module to load and the module takes care of the view and controller. The problem is I have the sidebar navigation menu hardcoded, and I want to make this dynamic. My first idea was to build a database of what modules were available (simply by running a check to see what files were in the module directory and running checks against them to determine that they fit the program), then after running through this list I would have a static displayMenu() method in each of these modules that could be called to build the menu.

The problem I ran into is calling these methods dynamically, assuming that the super class has no idea what their names are. I wrote a quick three second test to see if I could assign their names to a variable and call them via that;

PHP Code:

class static_var_test
{

    public static function 
displayMenu () {
        echo 
"worked";
    }
}

$var 'static_var_test';

$var::displayMenu(); 

...very basic, just to see if I could do something like this, but apparently not. Suggestions?
-m

Salathe 06-20-2008 07:09 PM

Before PHP 5.3.0 you can use:
call_user_func(array($var, 'displayMenu'));

With PHP 5.3.0 and above you can use what you tried and it'll work fine.

delayedinsanity 06-20-2008 08:42 PM

Much appreciated, I'm running 5.2.5 locally, if I recall correctly so is my server and I want to make this backwards compatible atleast to an extent, so I think I'll go with call_user_func().

In this case though wouldn't I want call_user_method()? I remember glossing over that in one of my reference manuals, IIRC it said they acted the same... I'll give it a go here.
-m

delayedinsanity 06-20-2008 08:46 PM

It would appear php.net isn't responding to me all day today... thankfully dreamweaver has a pocket reference built in, though it's hardly concise. So apparently if I wanted to use call_user_method() instead it would be like so;

PHP Code:

call_user_method('displayMenu'$var); 

However, as I'm sure Salathe will already know, that works but produces the following;


Strict Standards: Function call_user_method() is deprecated in C:\Users\marcus\Documents\My Web Sites\htdocs-tuatara\test\index.php on line 13

-m

Salathe 06-20-2008 09:11 PM

The call_user_method() function is deprecated as of PHP 4.1.0.

Enfernikus 06-20-2008 10:33 PM

ca.php.net or us.php.net is available.


All times are GMT. The time now is 07:40 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0