Page 1 of 1

counting array items belonging to a class object - works?

Posted: Thu Apr 10, 2003 1:35 pm
by torpedo51
I am counting items that belong to each object in a class array. It appears to work, but when I enable error_reporting(E_ALL) to make sure, I get the following error:

Undefined property: aryTxns [in file on line nnn]

Class looks roughly like this:
Resource[]->aryTxns[]->property1 ...

Array of class objects looks like this:
$aryResources

The code to perform the count the transactions owned by each resource:
$intTotalTxns += count($aryResources[$intLoopCtr]->aryTxns);

Do we consider the use of array functions on class objects as operative, and force ourselves to ignore the warnings?

TIA

Posted: Fri Apr 11, 2003 6:25 am
by volka

Code: Select all

<pre><?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
class Property
{
	var $whatever;	
	function Property($x,$y)
	{
		$this->whatever = $x . '/' . $y;
	}
}

class Txns
{
	var $aryTxns = array();
	function Txns($i)
	{
		for($j=0; $j!=$i; $j++)
			$this->aryTxns[] = new Property($i, $j);
	}
}

$aryResources = array();
for($i=0; $i!=10; $i++)
	$aryResources[] = new Txns($i);
	

$count = 0;	
for($i=0; $i!=count($aryResources); $i++)
{
	$count += count($aryResources[$i]->aryTxns);
	print_r($aryResources[$i]->aryTxns);
}
	
echo $count;
?></pre>
works for me without errors/warnings.