Re: variable object creating

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




----------  Message transmis  ----------

Subject: Re:  variable object creating
Date: Mercredi 22 Juin 2005 14:19
From: olivier <olivier.devaine@xxxxxxxxx>
To: php-general@xxxxxxxxxxxxx

Sorry for typo error, just need my cup of cofee...

Here is a good post for:
http://fr.php.net/manual/fr/function.call-user-func-array.php

################################################################
from taylor
08-May-2005 12:04
<?php
   /**
     * Create an object of a specified type using an array as the parameters
     * to the constructor.  NOTE: does not maintain proper
     * types for the arguments.  They are all converted to strings.
     * @param $type Type type of object to create (class name)
     * @param $args The arguments to pass to the constructor
     */
   function createObjArray($type, $args=array()) {
       if ( !class_exists($type) ) {
           return NULL;
       }

       // build argument list; be sure to escape string delimeters
       $func = create_function('$str', 'return str_replace("\'","\\\'",
$str);');
       $sargs = "'" . join( "','", array_map($func,$args) ). "'";

       // build & eval code; return result
       $seval = "return new $type($sargs);";
       return eval($seval);
   }
?>
#################################################################

I dont like eval too but i think that is depending on the pb we want to
solve... if you can change contrutor from each object may be better to use
jockem solution that is more secure (added some change for php4).

#################################################################
class Test
{
   var $v;
   // constructor for php4
   function Test($args){
       $this->__construct($args);
   }
   // constructor for php5
   function __construct($args = array())
   {
       extract((array) $args);
       $this->v=$v;
   }
}

$className = "Test";
$construct_params = array("v"=>"param1","param2","param3");

//if (class_exists($className, false)) { ---> for php5
if (class_exists($className)) { // for php4
    $obj = new $className($construct_params);
} else {
    die("Hack off mate.");
}

var_dump($obj);
##################################################################

>      // I had to test this to see if it works! the first 2 attempts are
> bogus // but you can run them to see what happens -- also the 3 attempt is
> a pretty // weird construction and I would be interested to know if anybody
> has thoughts // on calling the ctor in this way (essentially calling it
> twice) //

Never see a such solution, but may use a register funct instead...

##################################################################
class Test
{
   var $v;

   function Register($objName, $v=1){
      # auto register object if needed
      if(!isset($GLOBALS[$objName])){
             $GLOBALS[$objName]=true;
             $GLOBALS[$objName]=new Test($v);
       }else{
            return $GLOBALS[$objName];
       }
   }

   function Test($v){
       $this->__construct($v);
   }

   function __construct($v)
   {
       $this->v=$v;
   }
}

$className = "Test";
$construct_params = array("obj", "param1","param2","param3");
//if (class_exists($className, false)) { ---> for php5
if (class_exists($className)) { // for php4
   call_user_func_array(array(($className), "Register"), $construct_params);
} else {
    die("Hack off mate.");
}

var_dump($obj);
##################################################################

Love phpsec too ;-)
Hope this finaly help!
Olivier

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux