473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing constant and static members of a class name held in a variable

Does anyone please know of a way to access static methods of a class, when
the name of that class is held in a variable?

I have several classes (PHP5) which all have identically named methods and
members.

I want to pass the name of one of the classes to an included file which
does some standard operations using those methods and members. (So several
different programs can include that file, each passing a different class
name.)

I am doing this by putting the name of one of the classes in a variable
which is passed to the included file. So for example:
$classname = 'Foo';

$instance = new $classname();
$instance->do_method($id) ;

That all works fine.

But how do I access static methods and constant members of the class?

$classname::sta tic_method()
and
$classname::MY_ CONSTANT
both give errors. Every variation I've tried gives an error. Is there
something I'm missing in the syntax which will let me do this? Or maybe a
different method of passing the name, or of approaching the while thing?

I've found two ways of kludgeing round the problem. One is to use eval()
round everything, and the other (untested) is to use Reflection. But
surely there's a less messy way?

--
To reply email rafe, at the address cix co uk
Mar 3 '06 #1
2 2621

Rafe Culpin wrote:
Does anyone please know of a way to access static methods of a class, when
the name of that class is held in a variable?

I have several classes (PHP5) which all have identically named methods and
members.

I want to pass the name of one of the classes to an included file which
does some standard operations using those methods and members. (So several
different programs can include that file, each passing a different class
name.)

I am doing this by putting the name of one of the classes in a variable
which is passed to the included file. So for example:
$classname = 'Foo';

$instance = new $classname();
$instance->do_method($id) ;

That all works fine.

But how do I access static methods and constant members of the class?

$classname::sta tic_method()
and
$classname::MY_ CONSTANT
both give errors. Every variation I've tried gives an error. Is there
something I'm missing in the syntax which will let me do this? Or maybe a
different method of passing the name, or of approaching the while thing?

I've found two ways of kludgeing round the problem. One is to use eval()
round everything, and the other (untested) is to use Reflection. But
surely there's a less messy way?

--
To reply email rafe, at the address cix co uk


You can get to them using call_user_func( ) and constant() like this:

class foo {
const BAR = 5;
public static function baz() {
//do something static
}
}

$myClassName = 'foo';
$bar = constant($myCla ssName . '::BAR');
$z = call_user_func( array($myClassN ame, 'baz'));

Mar 3 '06 #2
In article <11************ **********@v46g 2000cwv.googleg roups.com>,
ze********@gmai l.com (ZeldorBlat) wrote:
You can get to them using call_user_func( ) and constant() like this:

class foo {
const BAR = 5;
public static function baz() {
//do something static
}
}

$myClassName = 'foo';
$bar = constant($myCla ssName . '::BAR');
$z = call_user_func( array($myClassN ame, 'baz'));


Aha! That works great - many thanks.

Seems odd they haven't made it simpler to do. Maybe in the next version.

--
To reply email rafe, at the address cix co uk
Mar 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4820
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant to be? Am I not suppose not to have any constant data member if I am going to have the assignment operator working for the class? Or am I...
4
6113
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is, its name can be used only by members and friends of the class in which it is
9
2296
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts that the app uses. So I will need to change the fonts depending on what settings the user has entered. However, it seems kind of wasteful to me to go...
10
2894
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class SomeClass { public SomeClass() { abc++; // Instance Variable xyz++; // Static Variable
0
1290
by: rain_c1 | last post by:
hi! is there a way to access a static member of a class, if the classname is a variable? this does not work: $classname::$member for methods there is a solution: call_user_func(array($classname, 'method'));
3
20836
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my attempts so far have been unsuccessful. Take this Test class as an example // test.hpp
9
8868
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've *defined* "x"'s value to be 10, isn't above statement a
6
8137
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a situation where I am implementing base class functions by including a pointer to subclass member in base class. Reason being functionality is...
7
4250
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant expression") or Comeau Online ("constant value is not known"). If I replace
0
7478
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...
0
7410
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...
0
7923
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...
1
7437
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...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4960
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1901
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
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.