instanciate class from array

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

    instanciate class from array

    Hello there!

    I'm searching a way to instanciate php5 class which names are
    contained in a array.
    Something like:
    $class_name = myArray['class'] ;
    $object = new $class_name().. . of course, this syntax won't work..
    that was just to explain :)
    I use the magic __autoload function to include required classes, but I
    can't find a way to instanciate a class from a string contained in a
    array..
    any idea ?

    thanks a lot :)

  • gosha bine

    #2
    Re: instanciate class from array

    On 25.06.2007 16:35 PaowZ wrote:
    Hello there!
    >
    I'm searching a way to instanciate php5 class which names are
    contained in a array.
    Something like:
    $class_name = myArray['class'] ;
    $object = new $class_name().. . of course, this syntax won't work..
    Just try it. ;)


    that was just to explain :)
    I use the magic __autoload function to include required classes, but I
    can't find a way to instanciate a class from a string contained in a
    array..
    any idea ?
    >
    thanks a lot :)
    >

    --
    gosha bine

    extended php parser ~ http://code.google.com/p/pihipi
    blok ~ http://www.tagarga.com/blok

    Comment

    • james.gauth@googlemail.com

      #3
      Re: instanciate class from array

      On 25 Jun, 15:35, PaowZ <gpa...@gmail.c omwrote:
      Hello there!
      >
      I'm searching a way to instanciate php5 class which names are
      contained in a array.
      Something like:
      $class_name = myArray['class'] ;
      $object = new $class_name().. . of course, this syntax won't work..
      that was just to explain :)
      I use the magic __autoload function to include required classes, but I
      can't find a way to instanciate a class from a string contained in a
      array..
      any idea ?
      >
      thanks a lot :)
      Actually, the syntax you described does work:

      <?php

      class foo {
      }

      class bar {
      }

      $classes = array('foo', 'bar');

      $object = new $classes[0];
      print_r($object );

      $object = new $classes[1];
      print_r($object );

      ?>

      Comment

      • PaowZ

        #4
        Re: instanciate class from array

        On 25 juin, 16:46, james.ga...@goo glemail.com wrote:
        On 25 Jun, 15:35, PaowZ <gpa...@gmail.c omwrote:
        >
        Hello there!
        >
        I'm searching a way to instanciate php5 class which names are
        contained in a array.
        Something like:
        $class_name = myArray['class'] ;
        $object = new $class_name().. . of course, this syntax won't work..
        that was just to explain :)
        I use the magic __autoload function to include required classes, but I
        can't find a way to instanciate a class from a string contained in a
        array..
        any idea ?
        >
        thanks a lot :)
        >
        Actually, the syntax you described does work:
        >
        <?php
        >
        class foo {
        >
        }
        >
        class bar {
        >
        }
        >
        $classes = array('foo', 'bar');
        >
        $object = new $classes[0];
        print_r($object );
        >
        $object = new $classes[1];
        print_r($object );
        >
        ?>
        well..hm.
        I gotta give a try before saying bullshit :)
        PHP parser is so nice....

        thanks :)

        Comment

        Working...