
January 28th, 2003, 10:08 AM
|
|
Junior Member
|
|
Join Date: Jan 2003
Posts: 8
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Best practice for objects returning objects
I making a set o classes that one of them returns a desired object.
I want to know what you think about this...
PHP Code:
class class_object
{
var $mObjects;
function class_object()
{
$this->mObjects = array();
}
function &addObject(& $object)
{
$this->mObjects[$object->getName()] =& $object;
return $object;
}
}
$object = new class_object()
$newObject =& $object->addObject(new other_object('name_here'));
Or i store all the possible classes i may add in an array...
PHP Code:
class class_object
{
var $mObjects;
var $mAllowedObjects;
function class_object()
{
$this->mObjects = array();
$this->mAllowedObjects = array
(
'type' => 'object_name',
'...' => '...'
);
}
function &addObject($type,$name)
{
if ($this->mAllowedObjects[$type])
{
$this->mObjects[$object->getName()] =&
new $this->mAllowedObjects[$type]();
return $object;
}
}
$object =& new class_object()
$newObject =& $object->addObject('object_type','name_here');
This way i have to change the class_object every time i decide to add a new object type.
What is your opinion about this?
thanks 
|