473,480 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

OOP calling static method of unknown class

29 New Member
Hi!

PHP is really nifty in the way you can instantiate classes by using string variables like so:

Expand|Select|Wrap|Line Numbers
  1. $str_class_name = 'User';
  2. $instance = new $str_class_name();
  3.  
That way you can create flows where classes that are used in the same way can use the same code even if you don't know which class will use the code beforehand.

My question is whether there is a way to do this for static methods of a class.

so something like:
Expand|Select|Wrap|Line Numbers
  1. class User {
  2.     public static function get_all(){
  3.         return "calling get_all from user";
  4.     }
  5. }
  6.  
  7. $class_name = 'User';
  8. $class_name::get_all();
  9.  
The above example would of course get all users but it does not work for obvious reasons. Does anyone know a way to accomplish what I'm asking?

best regards
Rythmic
Oct 25 '10 #1
1 2548
Markus
6,050 Recognized Expert Expert
The above code does work for PHP 5.3 and newer. For older versions, you'll have to use something like: call_user_func( array('class', 'func') );

Mark.
Oct 25 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
15562
by: Edward Diener | last post by:
This simple code example gives me the message, "TypeError: 'staticmethod' object is not callable". class X(object): def Y(x): print x Y = staticmethod(Y) ad = { 1 : Y } def Z(self):...
3
4926
by: Doug | last post by:
We are calling a static method in a class part of the project. This is C#. If we pass variables we see a constant growth in memory that does not seem to stop. If we do not pass variables, the...
2
4259
by: Ge Cong | last post by:
I have a class class MyClass { private: static double a; static void computeA(){ a =....; }
3
1727
by: Bob | last post by:
I have an abstract class Thing which has a static method Thing GetThing(). Class Something inherits from Thing. SomeThing supplies static info for GetThing to get stuff from the database to create...
6
31717
by: gg9h0st | last post by:
i really wander what makes static method special? in fact i can access a non static method statically using '::' class aclass { function anonstatic() { echo 'non static'; } static...
2
1178
by: DBC User | last post by:
I have a static class with bunch of static methods. That is my common routines used spratically across the programs. I am wondering which is the best coding and performance wise good standard. Try...
8
1232
by: Bob Rock | last post by:
Hello, is there a way of retrieving the type of the class containing a static method from the method itself when it gets called? I have this situation: an abstact class that defines a static...
9
1839
by: gaya3 | last post by:
Hi all, How does the static method is called without creating any instance for that class?How does this functionality works?Thanks in Advance. -Thanks & Regards, Hamsa
1
2003
by: samdediacre | last post by:
Hi all, I have developped a very simple Windows service that references a managed .NET dll. the code within the dll contains a static class to read/write data from registry. So the OnStart()...
1
2780
by: svatauli1983 | last post by:
Hi all, can someone write me if is normal to have business layer of static methods. Example: from page.aspx.cs calling static method in other assembly (static methods doesnt use state variables)....
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.