$class::method() in PHP 5.2

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
david64
Forum Commoner
Posts: 53
Joined: Sat May 02, 2009 8:12 am
Location: Wales

$class::method() in PHP 5.2

Post 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?
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: $class::method() in PHP 5.2

Post by mattpointblank »

I'm new to classes, but can't you do $class->method(); ?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: $class::method() in PHP 5.2

Post 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

Code: Select all

$class::method()
but

Code: Select all

call_user_func(array($class,'method'))
might be what you need
User avatar
jgadrow
Forum Newbie
Posts: 22
Joined: Wed Jun 17, 2009 7:56 pm
Location: Cincinnati, Ohio
Contact:

Re: $class::method() in PHP 5.2

Post 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. :)
david64
Forum Commoner
Posts: 53
Joined: Sat May 02, 2009 8:12 am
Location: Wales

Re: $class::method() in PHP 5.2

Post 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:

Code: Select all

$name = 'Class';
$name::method
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();
david64
Forum Commoner
Posts: 53
Joined: Sat May 02, 2009 8:12 am
Location: Wales

Re: $class::method() in PHP 5.2

Post 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.
User avatar
jgadrow
Forum Newbie
Posts: 22
Joined: Wed Jun 17, 2009 7:56 pm
Location: Cincinnati, Ohio
Contact:

Re: $class::method() in PHP 5.2

Post 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.
david64
Forum Commoner
Posts: 53
Joined: Sat May 02, 2009 8:12 am
Location: Wales

Re: $class::method() in PHP 5.2

Post 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 :)
Post Reply