How can you call static functions of a class without knowing its name in advance in PHP?
-
You can instantiate objects without knowing the class names in advance. Ex: <?php $a = "foo"; $b = new $a; //Works fine. ?> Is there a similar way to call static functions without knowing class names in advance
-
Answer:
This works: <?php $a = "ClassName"; $a::staticMethod(); ?>
Scot Lawrie at Quora Visit the source
Other answers
If you have an instance object of the class an question it can called it as a non static function works. <?php class Base { static function hello() { print("base_hello\n"); } } class Derived extends Base { static function hello() { print("derived_hello\n"); } } $instance = new Base(); $instance->hello(); $instance = new Derived(); $instance->hello(); ?> The output of the above is: base_hello derived_hello
Kartik Ayyar
Related Q & A:
- How can I disable default blocks in Drupal 7 without touching the Blocks Config admin?Best solution by Drupal Answers
- How can I call Android JavaScript functions in WebView?Best solution by Stack Overflow
- How can I make my voice high in videos without speeding the video itself up?Best solution by Yahoo! Answers
- How can I put custom firmware on my PSP without a Pandora battery?Best solution by Yahoo! Answers
- How can I get smooth,hair-free beautiful legs without ingrown hair?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.