php:对象的数组(1)
一、php源码
<?php
// define object
class obj{
public $v1 = "";
public $v2 = "";
public function init()
{
$this -> v1 = "";
$this -> v2 = "";
}
}
// create and init "obj" array
for($i=0; $i<10; $i++)
{
$cat[$i] = new obj;
$cat[$i]->v1 ="hello_v1_" . $i;
$cat[$i]->v2 ="hello_v2_" . $i;
}
// traverse array of obj
foreach($cat as $ct)
{
echo "v1: " . $ct->v1 . "\t" . "v2: " . $ct->v2 . PHP_EOL;
}
?>
二、php执行结果