PDA

View Full Version : [SOLVED] PHP sharing an array between several objects of diffrent classes



bluedalmatian
November 10th, 2012, 06:14 PM
I want to share a single array between multiple objects (of different classes) in PHP.

Each class has a constructor function which takes a ref to the array as an argument and assigns it to a local member variable so that other methods in the class can access it later, however I think when I assign the argument to the local variable Im ending up with a copy of the array.

I dont want this, I want to store a ref to the array so that if any of the objects add or remove an element all other objects will see those changes. This is my code:



function MemberPaymentDAO(&$themap) //constructor
{
$this->all_daos=$themap; //store a ref to the shared array
$this->all_daos['MemberPayment'] = $this; //add an element
}


In the class def $all_daos is simply decalred like this:


var $all_daos;


I suspect the def and the 1st line of the constructor need changing but PHP refs confuse me I much prefer C++ pointers - you know where you stand with them:)

pkadeel
November 10th, 2012, 08:38 PM
I want to share a single array between multiple objects (of different classes) in PHP.

Each class has a constructor function which takes a ref to the array as an argument and assigns it to a local member variable so that other methods in the class can access it later, however I think when I assign the argument to the local variable Im ending up with a copy of the array.

I dont want this, I want to store a ref to the array so that if any of the objects add or remove an element all other objects will see those changes. This is my code:



function MemberPaymentDAO(&$themap) //constructor
{
$this->all_daos=$themap; //store a ref to the shared array
$this->all_daos['MemberPayment'] = $this; //add an element
}
In the class def $all_daos is simply decalred like this:


var $all_daos;
I suspect the def and the 1st line of the constructor need changing but PHP refs confuse me I much prefer C++ pointers - you know where you stand with them:)
You need to assign a reference to your class variable like


$this->all_daos=&$themap; //stores a ref to the array $themap

bluedalmatian
November 12th, 2012, 12:39 AM
thank you