note 81393 added to language.oop5.autoload

From: Date: Tue, 26 Feb 2008 11:23:19 +0000
Subject: note 81393 added to language.oop5.autoload
Groups: php.notes 
Request: Send a blank email to php-notes+get-136492@lists.php.net to get a copy of this message
define( 'SYSTEMROOT', 'classes' ); /** * PHP 4 alternative class autoloading function, * params: useable with strings, integers, arrays and sub-arrays * * @param $classname The name of the class to instantiate * @param $params Array of parameters to pass to the class constructor. Primitive types only * @return object */ function &cl( $classname, $params = null ) { if( !class_exists( $classname )) { require_once( SYSTEMROOT . '/' . $classname . '.class.php' ); } if( $params != null ) { // build params for object $newParams = array(); foreach($params as $key=>$value) { $test = strtolower(substr($value,0,5)); if($test=='array') { eval( "\$myArray = array( ".substr($value, strpos($value, '(') + 1, strrpos($value, ')') - strpos($value, '(') - 1)." );" ); $newParams[$key] = $myArray; } else { $newParams[$key] = $value; } } // build string for object $count = 0; $strParams = ''; foreach($newParams as $keyParam => $valueParam) { $count++; $strParams .= "\$newParams[$keyParam]"; if($count < count($newParams)) { $strParams .= ","; } } // test object params eval( "\$object =& new $classname($strParams);" ); // return with params return $object; } return new $classname; } $foo =& cl('testclass', array( 'array( "abe"=>1, 2, hmm , "coool"=> 4,5,6=>array("niiice"=>"cooper","good!","great"))' , '222', 333, "im not used in object" ) ); echo "<pre>"; print_r($foo); ---- Server IP: 195.184.96.15 Probable Submitter: 129.142.143.18 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php Edit -- https://master.php.net/note/edit/81393 Del: integrated -- https://master.php.net/note/delete/81393/integrated Del: useless -- https://master.php.net/note/delete/81393/useless Del: bad code -- https://master.php.net/note/delete/81393/bad+code Del: spam -- https://master.php.net/note/delete/81393/spam Del: non-english -- https://master.php.net/note/delete/81393/non-english Del: in docs -- https://master.php.net/note/delete/81393/in+docs Del: other reasons-- https://master.php.net/note/delete/81393 Reject -- https://master.php.net/note/reject/81393 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136492) next »