473,666 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

$result = $className::$me thodName(); - possible?

Hi All,

I have been trying to dynamically call a static member function, as follows:

$className = 'MyClass';
$methodName = 'MyMethod'
$result = $className::$me thodName();

However, I get a parse error when I do this:

Parse error: parse error, unexpected T_PAAMAYIM_NEKU DOTAYIM in
/home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrati onBridgerClasse s.inc
on line 28

Line 28 is indeed the line in which $result = $className::$me thodName();
appears.

Is there any way to do what I am trying to do? I have had success
dynamically calling methods on known classes. E.g.,

$methodName = 'MyMethod';
$result = MyClass::$metho dName();

This works fine. What if I want to have the class name dynamically defined
as well? Is there any way to do that?

Thanks for any pointers!
Jul 17 '05 #1
7 2846
.oO(Joshua Beall)
Is there any way to do what I am trying to do? I have had success
dynamically calling methods on known classes. E.g.,

$methodName = 'MyMethod';
$result = MyClass::$metho dName();

This works fine. What if I want to have the class name dynamically defined
as well? Is there any way to do that?


call_user_func( ) should help.

Micha
Jul 17 '05 #2
Joshua Beall wrote:
Hi All,

I have been trying to dynamically call a static member function, as follows:

$className = 'MyClass';
$methodName = 'MyMethod'
$result = $className::$me thodName();

However, I get a parse error when I do this:

Parse error: parse error, unexpected T_PAAMAYIM_NEKU DOTAYIM in
/home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrati onBridgerClasse s.inc
on line 28

Line 28 is indeed the line in which $result = $className::$me thodName();
appears.

Is there any way to do what I am trying to do? I have had success
dynamically calling methods on known classes. E.g.,

$methodName = 'MyMethod';
$result = MyClass::$metho dName();

This works fine. What if I want to have the class name dynamically defined
as well? Is there any way to do that?

Thanks for any pointers!


In your code is the semicolon(;) missing or is that a typo?
$className = 'MyClass';
$methodName = 'MyMethod'; <---- semicolon
$result = $className::$me thodName();
Jul 17 '05 #3
"Jay Holyer-Riviere" <ja*@loudmouthe d.net> wrote in message
news:10******** *****@corp.supe rnews.com...
In your code is the semicolon(;) missing or is that a typo?
$className = 'MyClass';
$methodName = 'MyMethod'; <---- semicolon
$result = $className::$me thodName();


Oops, typo - sorry about that.

I simplified my code for the sake of example, and I missed that semicolon.
Jul 17 '05 #4
"Michael Fesser" <ne*****@gmx.ne t> wrote in message
news:d1******** *************** *********@4ax.c om...
.oO(Joshua Beall)
Is there any way to do what I am trying to do? I have had success
dynamically calling methods on known classes. E.g.,

$methodName = 'MyMethod';
$result = MyClass::$metho dName();

This works fine. What if I want to have the class name dynamically
defined
as well? Is there any way to do that?


call_user_func( ) should help.


call_user_func( array($classNam e,$methodName), $parameter); did the trick.
Thanks for the pointer, Michael.
Jul 17 '05 #5
Joshua Beall wrote:
Is there any way to do what I am trying to do? I have had success
dynamically calling methods on known classes. E.g.,

$methodName = 'MyMethod';
$result = MyClass::$metho dName();

This works fine. What if I want to have the class name dynamically
defined as well? Is there any way to do that?


$className = 'MyClass';
$methodName = 'MyMethod'
$result = call_user_func( array($classNam e, $methodName));
JW

Jul 17 '05 #6
"Joshua Beall" <jb****@donotsp am.remove.me.he raldic.us> wrote in message
news:HWXHd.1072 $BL3.484@trnddc 01...
Hi All,

I have been trying to dynamically call a static member function, as follows:
$className = 'MyClass';
$methodName = 'MyMethod'
$result = $className::$me thodName();

However, I get a parse error when I do this:

Parse error: parse error, unexpected T_PAAMAYIM_NEKU DOTAYIM in
/home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrati onBridgerClasse s.in
c on line 28


There's a workaround, at least:

$result = call_user_func( array($classNam e,$methodName), <any parameters
here>);

Regards,

Terje
Jul 17 '05 #7
Joshua Beall scribbled something along the lines of:
Hi All,

I have been trying to dynamically call a static member function, as follows:

$className = 'MyClass';
$methodName = 'MyMethod'
$result = $className::$me thodName();

However, I get a parse error when I do this:

Parse error: parse error, unexpected T_PAAMAYIM_NEKU DOTAYIM in
/home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrati onBridgerClasse s.inc
on line 28

Line 28 is indeed the line in which $result = $className::$me thodName();
appears.

Is there any way to do what I am trying to do? I have had success
dynamically calling methods on known classes. E.g.,

$methodName = 'MyMethod';
$result = MyClass::$metho dName();

This works fine. What if I want to have the class name dynamically defined
as well? Is there any way to do that?

Thanks for any pointers!

The problem is that $className::$me thodName() results in an error simply
because $className is a string, not an object.
$$className won't work because $MyClass is an undefined variable and not
an object.

I think eval() might solve that.
Try something like

eval($className .'::'.$methodNa me.'();');

This *should* translate the string into

MyClass::MyMeth od();

and execute it.

I'm sure that eval() will help you out but I haven't tested this.

--
Ashmo
Jul 17 '05 #8

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

Similar topics

39
3317
by: Mark Johnson | last post by:
It doesn't seem possible. But would the following also seem a violation of the general notions behind css? You have a DIV, say asociated with class, 'topdiv'. Inside of that you have an anchor (and whatever it contains), associated with class, 'a'. Somewhere else in, 'topdiv', there is a span or element (containing whatever), associated with class, 'b'.
2
6320
by: mike.bratanata | last post by:
I have a table as such: <table> <tr> <td>Col 1</td> <td>Col 2</td> </tr> <tr class="bgGrey"> <td>Col1Row1</td> <td>Col2Row1</td> </tr>
4
1368
by: Sahil Malik | last post by:
Lets say .. I get a string .. "System.Windows.Forms.TextBox" Can I instantiate an instance of that .. given that the ONLY information I have is that string above i.e. classname? To make things easy, such a classname is in GAC. - Sahil Malik http://codebetter.com/blogs/sahil.malik/
1
1901
by: Rene Sørensen | last post by:
I'm trying to make things alot easyer for my self, by useing a formatet error report, but i need some info to do that, like etc... MessageBox.Show( "Error In File: " + GetFileNAme() + " \n" + "Class Name: " + GetClassName() etc..... i have been looking at StackFrame, and this is ok, but only useable in debug mode, anyone have any idea how to do this is release mode??? Thnks in Advanced
1
3609
by: Supernaut | last post by:
I was just battling a small dHTML quirk where I was attempting to set the className attribute of option elements, but it didn't seem to be updating. I did some testing with other elements and came to find the problem was not in my code at all, but rather in my logic... I had a dehighlightAll(selectBox) function in my code library and decided to use this to reset all the options to white backgrounds. This worked fine with my previous...
11
10877
by: Martin Höfling | last post by:
Hi there, is it possible to put the methods of a class in different files? I just want to order them and try to keep the files small. Regards Martin
18
6762
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work, until I subclassed a class using this technique and from there on things started screwing up. I finally tracked it down to self.__class__.attribute! What was happening is that the child classes each over-rode the class attribute at their level,...
7
1830
by: uncle | last post by:
config file that contains lines like... assemblyname|classname|methodname I want to invoke that static method. I found the following examples which make sense... Type theClass = assembly.GetType("Namespace.Classname", false, true); bool result = (bool)theClass.InvokeMember("Initialize", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static,
1
2450
by: Steph | last post by:
hello, how can i redefine the "<MethodName>Response" and "<MethodName>Result" .... in the soap response ! "<MethodName>Response" = ResponseElementName "<MethodName>Result" = ? thanks
0
8449
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
8360
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
8876
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
8784
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
8556
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
7387
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1777
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.