Forum Moderators: coopster

Message Too Old, No Replies

how to avoid collision of objects ?

         

PHPycho

10:21 am on Apr 2, 2008 (gmt 0)

10+ Year Member



Hello forums!
Suppose we had the following classes:
[PHP]class A{
var $obj_array = array();
function setObject($classB_obj){
$this->obj_array[] = $classB_obj
}
}

class B{
var $id;
var $name;
function B($id, $name){
// necessary initialisation
}
}[/PHP]
Case:
I would like to set objects as:
[PHP]$objA->setObject(new B(1, "x"));
$objA->setObject(new B(2, "y"));[/PHP]
My Question:
How to make the property $obj_array to hold the class objects of B with unique id.

Thanks in advance for the kind help.

PHP_Chimp

11:16 am on Apr 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I dont understand exactly what you are after, but have you looked at the extends [uk3.php.net] method? As this allows you to build classes on top of each other. So your class A would be the base class then you can use all of that in class B.

class A { // I know I have swapped around your classes, its just so that B extends A
var $id;
var $name;
function A($id, $name){
// necessary initialisation
}
class B extends class A {
var $obj_array = array();
function B($id, $name) {
$this->A($id, $name);
}

The only thing with this is that there can be problems getting the constructor from the parent class to load, as you need to call it explicitly in the child class.

If $this->A doesnt work then try -
parent::A
A::A