You can do arrays of classes, but it is not immediately obvious how you do it. I tried for many hours to figure it out until I ran across some code that made me think about the solution.
Basically you can do arrays ONLY if you have class methods (functions) that access all parts of the class that is in the array. They instead of trying to access the variable in the class, use its method (function) to access the value. If you just try to get to the value you will get a bad value, but using the method will allow you to access the variable's value.
Like this:
<?
class cart
{
var $a;
function a ($val = 0)
{
if ($val)
$this->a = $val;
else
return $this->a;
}
}
var $x = array();
$x[0] = new cart;
$x[1] = new cart;
$x[0]->a(10);
$x[1]->a(20);
$y = $x[0]->a();
$z = $x[1]->a();
?>
That's about it.
Good luck!
Christi wrote:
Is it possible to make a class instance into an array?
Ex. (syntax might not be correct)
class cart
{
var $id;
var $data;
}
$storage[] = new cart;
$storage[0]->id = 1;
$storage[1]->id = 2;
$storage[2]->id = 3;
Basically, I need to use a class so I can keep associated data encapsulated, but since this data is coming from a continuously changing database I need to be able to turn one instance of the class into an array. Thanks.