Reply to Re: Dynamic Object Function Call in PHP

Your name:

Reply:


Posted by Jerry Stuckle on 05/05/06 05:31

aroldao@gmail.com wrote:
> Greetings Everyone,
>
> In php it's possible to create a new object based on a name stored
> in variable, i.e:
>
> $className = "CSome$name";
> $newclass = new $className();
>
> However is it possible to call a function from a class using a name
> stored in a variable? For example:
>
> $funcname = "operation";
> $func= "\$someclass->$funcname";
>
> $func( arg1, arg2 );
>
> With the same result as
>
> $someclass->operation( arg1, arg2 );
>
> Any feed-back will be much appreciated.
>
> Cheers,
> Roldao
>

Hi, Roldao,

Well, you have a problem. To call a non-static method in a class, you must have
an object of the class - not the name of the class.

So, for instance:

class MyClass {
function operation() {
...
}
}

MyClass->operation()

will fail.

However, if you do

$myclass = new MyClass();
$myclass->operation();

You can also call it indirectly, with

$function = "operation";

call_user_func(array($myclass), $function);

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация