Page 1 of 1
$class::method() in PHP 5.2
Posted: Thu Jun 18, 2009 11:17 am
by david64
Code: Select all
$class = "ClassName";
$class:method();
I want to do that in PHP 5.2.6, but its only been available since PHP 5.3. Does anyone have any recommended work arounds?
Re: $class::method() in PHP 5.2
Posted: Thu Jun 18, 2009 11:28 am
by mattpointblank
I'm new to classes, but can't you do $class->method(); ?
Re: $class::method() in PHP 5.2
Posted: Thu Jun 18, 2009 11:34 am
by Mark Baker
mattpointblank wrote:I'm new to classes, but can't you do $class->method(); ?
Only if you've instantiated that class using $class = new ClassName().
david64 wrote:I want to do that in PHP 5.2.6, but its only been available since PHP 5.3. Does anyone have any recommended work arounds?
A double colon might help
but
Code: Select all
call_user_func(array($class,'method'))
might be what you need
Re: $class::method() in PHP 5.2
Posted: Thu Jun 18, 2009 11:41 am
by jgadrow
The only thing that comes to mind is writing it to a string and running it through eval (). Not even sure if that would work properly and, of course, you should always use eval () with care.
This is one of the reasons php 5.3 is going to re-define the ways this version of PHP is utilized.
Hope that helps! And, please, post here if it worked or not so it will be available to future searchers.

Re: $class::method() in PHP 5.2
Posted: Thu Jun 18, 2009 11:49 am
by david64
I should have been a bit more clear. I have a class with a static method like:
Code: Select all
class
{
static function method() { ... }
}
and I wanted to be able call it with a variable, like:
However, this is not available in PHP 5.2.6, but is available in PHP 5.3.
I did have a look at call_user_func(), but from reading the documentation it seemed that it could only help if I was on PHP 5.3, such as this:
Code: Select all
call_user_func(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0
However, what Mark has suggested has worked:
Code: Select all
$name = "XFL_Validate";
call_user_func( array( $name, 'test' ), 1, 2 )
Thanks Mark
@mattpointblank - you can run methods like that. That is how you run methods on classes that have been created as objects ($foo = new Class). However, you can also call methods without creating a class as a object using ClassName::method();
Re: $class::method() in PHP 5.2
Posted: Thu Jun 18, 2009 11:53 am
by david64
jgadrow wrote:The only thing that comes to mind is writing it to a string and running it through eval (). Not even sure if that would work properly and, of course, you should always use eval () with care.
Yeah. Luckily Mark's suggestion did the trick. eval() is a last resort botch. I am sure eval() would work. Everything I've tried to do with eval() has worked; although it might not be obvious how to phrase it correctly.
Re: $class::method() in PHP 5.2
Posted: Thu Jun 18, 2009 11:58 am
by jgadrow
Mark must have posted while I was typing mine. I agree, his solution is MUCH better!
Would be nice if this forum notified me of a new post before it performed the final post on my entry... Oh well.
Re: $class::method() in PHP 5.2
Posted: Thu Jun 18, 2009 12:15 pm
by david64
As a not to anyone who might use those thread as a reference using the call_user_func method uses about another 400 bytes of memory over a straight forward call. If you are developing something to be used across more than one server you may want to take advantage of the enhancements in PHP 5.3 and use this as a fallback to save a few Kb
