473,602 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ 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 2561
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
15585
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): self.ad(3)
3
4947
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 memory usage does not grow. We understand GC is supposed to take care of releasing memory, but we started to go over 300,000,000 and decided this was not OK. Any thoughts/help is appreciated.
2
4267
by: Ge Cong | last post by:
I have a class class MyClass { private: static double a; static void computeA(){ a =....; }
3
1737
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 a Something. Now I have a collection Things<T>:List<T> where T:Thing. In that collection's code how can I call Thing's static method? T.GetThing gives a compile error. tempThing = new T();
6
31733
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 function astatic() {
2
1187
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 to put the required methods in a class and use the class or keep the static on most commonly used methods? Thanks.
8
1237
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 method, a class that derives from the abstract class and I need to retrieve the type of the concrete class when calling the static method. public abstact class MyAbstact
9
1848
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
2025
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() method of the service simply calls the dll to read info from registry, but the static method reading info fails to execute... the dll tested within a console or winform application works fine (i can call the static class and get the info i want) So i...
1
2785
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). Can be some problem when more requests will be calling that static method in same time? And How static methods works? That method is created (new instance of some object) when it is calling and in the end is disposed? Thanks
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5867
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
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 we have to send another system

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.