counting array items belonging to a class object - works?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
torpedo51
Forum Newbie
Posts: 3
Joined: Thu Mar 13, 2003 2:11 am
Location: Surf City, CA

counting array items belonging to a class object - works?

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply