PHP dynamic class instantiation like Java

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roger Vandervort

    PHP dynamic class instantiation like Java

    I've leafed through the docs and user comments on php.net, but haven't
    been able to find anything related to this.

    Is there a way to dynamically instantiate a class, like you can in
    Java with class.forName() ?
    example:

    $myclass = "Automobile ";
    $func = "drive";

    $myInstance = new $myclass;
    call_user_metho d ($func, $myInstance); // i know this is deprecated.
  • Louis-Philippe Huberdeau

    #2
    Re: PHP dynamic class instantiation like Java

    Create a factory-like method that will return an instance of the class
    and use call_user_func( ) with an array as the first parameter
    (containing the class name and your factory method)

    class foo
    {
    function factory()
    {
    return new foo();
    }
    }

    $obj = call_user_func( array('foo', 'factory') );

    --
    Louis-Philippe Huberdeau

    Roger Vandervort wrote:[color=blue]
    > I've leafed through the docs and user comments on php.net, but haven't
    > been able to find anything related to this.
    >
    > Is there a way to dynamically instantiate a class, like you can in
    > Java with class.forName() ?
    > example:
    >
    > $myclass = "Automobile ";
    > $func = "drive";
    >
    > $myInstance = new $myclass;
    > call_user_metho d ($func, $myInstance); // i know this is deprecated.[/color]

    Comment

    • Roger Vandervort

      #3
      Re: PHP dynamic class instantiation like Java

      Louis-Philippe Huberdeau <lphuberdeau@sy mpatico.ca> wrote in message news:<qFJcb.656 1$1H3.424863@ne ws20.bellglobal .com>...[color=blue]
      > Create a factory-like method that will return an instance of the class
      > and use call_user_func( ) with an array as the first parameter
      > (containing the class name and your factory method)
      >
      > class foo
      > {
      > function factory()
      > {
      > return new foo();
      > }
      > }
      >
      > $obj = call_user_func( array('foo', 'factory') );
      >
      > --
      > Louis-Philippe Huberdeau
      >
      > Roger Vandervort wrote:[color=green]
      > > I've leafed through the docs and user comments on php.net, but haven't
      > > been able to find anything related to this.
      > >
      > > Is there a way to dynamically instantiate a class, like you can in
      > > Java with class.forName() ?
      > > example:
      > >
      > > $myclass = "Automobile ";
      > > $func = "drive";
      > >
      > > $myInstance = new $myclass;
      > > call_user_metho d ($func, $myInstance); // i know this is deprecated.[/color][/color]


      Actually, I did determine that this does work:

      class Automobile {
      function myFunction($ech oString) { echo 'echoing
      Automobile:'.$e choString; }
      }

      $myClass = "Automobile ";
      $func = "myFunction ";
      $param = 'Vroom...Vroom' ;
      $myInstance = new $myClass;
      call_user_funct ion(array(&$myC lass,$func),$pa ram); // will print
      'echoing Automobile:Vroo m...Vroom';

      Not sure which versions support this though.

      Comment

      Working...