我需要一些帮助。我有一个工人阶级,我可以使用 foreach() 来显示公共变量:
class MyClass {
public $a;
public $b;
function __construct($aX,$bX){
$this->a = $aX;
$this->b = $bX;
}
}
$class = new MyClass(3,5);
foreach($class as $key => $value) {
print "$key => $value</br>\n";
}
产生:
a => 3
b => 5
现在,当我想要一个 MyClass 数组时,我的问题就出现了:
class MyClass
{
public $a;
public $b;
function __construct($aX,$bX){
$this->a = $aX;
$this->b = $bX;
}
}
$class = array(
"odd"=>new MyClass(3,5),
"even"=>new MyClass(2,4)
);
foreach($class as $key => $value) {
print "$key => $value</br>\n";
}
产生:
Catchable fatal error: Object of class MyClass could not be converted to string...
如何循环遍历 $class 数组的所有元素?任何帮助都会很棒!