Bluehost.com Web Hosting $6.95

Dynamic Object Function Call in PHP

This is a discussion on Dynamic Object Function Call in PHP within the PHP Language forums, part of the PHP Programming Forums category; Greetings Everyone, In php it's possible to create a new object based on a name stored in variable, i....


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-05-2006
aroldao@gmail.com
 
Posts: n/a
Default Dynamic Object Function Call in PHP

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

Reply With Quote
  #2 (permalink)  
Old 05-05-2006
Chung Leong
 
Posts: n/a
Default Re: Dynamic Object Function Call in PHP

What you are trying to do isn't possible. PHP treat the variable in
$func() strictly as a text string--not evaluable code. If $func
contains "$someclass->operation" it will just look for a standalone
function called "$someclass->operation."

Reply With Quote
  #3 (permalink)  
Old 05-05-2006
Jerry Stuckle
 
Posts: n/a
Default Re: Dynamic Object Function Call in PHP

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
==================
Reply With Quote
  #4 (permalink)  
Old 05-05-2006
aroldao@gmail.com
 
Posts: n/a
Default Re: Dynamic Object Function Call in PHP

Thak-you for your feedback, both Jerry Stuckle and Chung Leong. Got it
sorted now.

Roldao


Jerry Stuckle wrote:
> 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
> ==================


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:45 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0