我尝试使用下面的PHP类:为什么这个php函数调用失败?
<?php
class Service {
public $code, $description;
public static $services = array(
"A" => "Shipping",
"B" => "Manufacturing",
"C" => "Legal",
"D" => "Accounts Receivable",
"E" => "Human Resources",
"F" => "Security",
"G" => "Executive",
"H" => "IT"
);
public function _construct($c, $d) {
$this->code = $c;
$this->description = $d;
}
public static function getDescription($c){
return $services[$c];
}
public static function generateServiceList() {
$service_list[] = array();
foreach ($services as $k => $v){
$service_list[] = new Service($k, $v);
}
return $service_list;
}
}
?>
...以下列方式:
<?php
$services = Service::generateServiceList();
?>
...但得到以下错误:
Warning: Invalid argument supplied for foreach() in /classes/service.php on line 31
任何想法为什么?这是一种访问问题吗?谢谢。
不能 - generateServiceList函数和数组都是静态的。 – rybosome
好的,自我:: $服务 – RiaD
是的,这是做到了。 – rybosome