473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

$result = $className::$methodName(); - possible?

Hi All,

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

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

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

Parse error: parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM in
/home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrationBridgerClasses.inc
on line 28

Line 28 is indeed the line in which $result = $className::$methodName();
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::$methodName();

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 2827
.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::$methodName();

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::$methodName();

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

Parse error: parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM in
/home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrationBridgerClasses.inc
on line 28

Line 28 is indeed the line in which $result = $className::$methodName();
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::$methodName();

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::$methodName();
Jul 17 '05 #3
"Jay Holyer-Riviere" <ja*@loudmouthed.net> wrote in message
news:10*************@corp.supernews.com...
In your code is the semicolon(;) missing or is that a typo?
$className = 'MyClass';
$methodName = 'MyMethod'; <---- semicolon
$result = $className::$methodName();


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.net> wrote in message
news:d1********************************@4ax.com...
.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::$methodName();

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($className,$methodName),$para meter); 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::$methodName();

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($className, $methodName));
JW

Jul 17 '05 #6
"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> wrote in message
news:HWXHd.1072$BL3.484@trnddc01...
Hi All,

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

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

Parse error: parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM in
/home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrationBridgerClasses.in
c on line 28


There's a workaround, at least:

$result = call_user_func(array($className,$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::$methodName();

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

Parse error: parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM in
/home/HE/web_develop/ghea/www/cgi-bin/lib/EventRegistrationBridgerClasses.inc
on line 28

Line 28 is indeed the line in which $result = $className::$methodName();
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::$methodName();

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::$methodName() 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.'::'.$methodName.'();');

This *should* translate the string into

MyClass::MyMethod();

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
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...
2
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
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...
1
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" +...
1
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...
11
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
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,...
7
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 =...
1
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
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,...
0
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...
0
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,...

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.