Dynamic Object Function Call in PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aroldao@gmail.com

    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= "\$someclas s->$funcname";

    $func( arg1, arg2 );

    With the same result as

    $someclass->operation( arg1, arg2 );

    Any feed-back will be much appreciated.

    Cheers,
    Roldao

  • Chung Leong

    #2
    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."

    Comment

    • Jerry Stuckle

      #3
      Re: Dynamic Object Function Call in PHP

      aroldao@gmail.c om wrote:[color=blue]
      > 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= "\$someclas s->$funcname";
      >
      > $func( arg1, arg2 );
      >
      > With the same result as
      >
      > $someclass->operation( arg1, arg2 );
      >
      > Any feed-back will be much appreciated.
      >
      > Cheers,
      > Roldao
      >[/color]

      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@attgl obal.net
      =============== ===

      Comment

      • aroldao@gmail.com

        #4
        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:[color=blue]
        > aroldao@gmail.c om wrote:[color=green]
        > > 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= "\$someclas s->$funcname";
        > >
        > > $func( arg1, arg2 );
        > >
        > > With the same result as
        > >
        > > $someclass->operation( arg1, arg2 );
        > >
        > > Any feed-back will be much appreciated.
        > >
        > > Cheers,
        > > Roldao
        > >[/color]
        >
        > 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@attgl obal.net
        > =============== ===[/color]

        Comment

        Working...