Dynamically created objects in PHP?

Frxstrem

New member
I have two questions. You only have to answer one of them, but answers to both would be great ;-)

First question: Say I want to make an object dynamically, like this:
<?php $object = (object) array('var1' => 'foo', 'var2' => 'bar'); ?>
or this:
<?php $object = new stdClass(); $object->var1 = 'foo'; $object->var2 = 'bar'; ?>
Is it possible to then add a function to that object, without having to assign it a class?

Second question: Is it possible to dynamically assign variable key names inside a class, for example:
<?php class SampleClass { function __construct($key, $value) { $this->[$key] = $value } } ?>
This is invalid PHP syntax, but I hope you still get the point.

Thanks,
~ Fredrik
 
Back
Top