Thomas Hochstetter wrote:
Spiraleye.Studios
Hi there,
I need help with arrays. What I want to do is to have an array of the
following structure:
$mod=array( ‘name’=>new NameObject());
^ -- looks like your single quotes got mangled in my email client.
class Test { function Test($str = '') { echo $str; } }
$_GET['module'] = 'testmod';
$mod = array( 'testmod' => 'Test' );
if (isset($mod[$_GET['module']]) && class_exists($c = $mod[$_GET['module']])) {
$Site = new $c;
// $Site = new $c('Testing');
} else {
die ('Can't create module object');
}
... and if that doesn't give you the knowledge/inspiration/lighbulb-moment
to fix whatever it is your trying to do then please mail the list again
and explain what _and_ why you are trying to do whatever it is you're trying to
do (this helps people to understand thereby having a better chance of
offering some helpful advice!)
Then later in the page I want to go $Site = $mod[$_GET[‘module’]] (or
something like that) to instantiate a new object.
The problem is, if done the way above it will try to instantiate the
object right there and then in the array (defeating the purpose), if I
put it in quotes and try to use something like:
(object)eval($mod[$_GET[‘module’]]) it does not instantiate the object.
well assuming no syntax error occurs (eval will return false is such a case)
then I bet it actually does, only your eval statement isn't assigning the
new object to anything!... besides which you are casting the return
value of eval to an object which is pointless for 2 reasons:
1. casting NULL to an object gets you nowhere (atleast not in this case)
2. casting an object to an object get you... nowhere :-)
so....:
$mod = array( 'name' => 'return new NameObject()');
$Site = eval($mod['name']);
PS - do you know what a reference is? (in terms of [php] variables)? understanding
references with regard to objects (especially if your using php4) is a really
good addition to your coding arsenal (it will save you time hunting inexplicable bugs -
often such 'bugs' are nog longer inexplicable! and allow you to increase the speed of your
code)
PPS - using eval() is hardly ever the only option, and given that eval() is very slow its best
to avoid it at all costs (assuming there is another way to do what you want)... also eval()
_can_ be a security risk, if you do something like:
eval($_GET['command'])
without santizing the contents of $_GET['command'], hopefully you can see why.
Is there a way to do this?
Thanks
Thomas
*
**SPIRAL EYE STUDIOS ***
P.O. Box 37907, Faerie Glen, 0043
Tel: +27 12 362 3486
Fax: +27 12 362 3493
Mobile: +27 83 258 2669
Email: info@xxxxxxxxxxxxxxx <mailto:info@xxxxxxxxxxxxxxx>
Web: www.spiraleye.co.za <http://www.spiraleye.co.za>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php