我需要一些幫助。我有一個工人階級和我可以用一個foreach()來顯示公共變量:PHP 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數組的所有元素?任何幫助將是偉大的!