TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-20-2008, 06:55 PM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default 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
delayedinsanity is offline  
Reply With Quote
Old 06-20-2008, 07:09 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
delayedinsanity (06-20-2008)
Old 06-20-2008, 08:42 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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 is offline  
Reply With Quote
Old 06-20-2008, 08:46 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
Old 06-20-2008, 09:11 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The call_user_method() function is deprecated as of PHP 4.1.0.
Salathe is offline  
Reply With Quote
Old 06-20-2008, 10:33 PM   #6 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

ca.php.net or us.php.net is available.
Enfernikus is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:20 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design