简单的OOP数组参数不返回值,而是说为foreach()提供了无效的参数

时间:2018-09-28 15:23:46

标签: php oop

简单的OOP数组参数不返回值,而是说为foreach()提供了无效的参数,我不知道为什么它不返回数组中的$ this-> data值

class MyClass{

    var $data;

    public function getData()
    {
        $data = array('Toyota'=>'Corolla','Honda' => 'Civic', 'Suzuki'=>'Mehran');
        $this->data = $data;
    }

    public function showData($comp)
    {

        foreach($this->data as $company => $model){

        if($company == $comp){
            return $comp.' is currently producing Model: '.$model;
             break;

            }
        }

    }

}
$cars = new MyClass();

echo $cars->showData('Honda');

1 个答案:

答案 0 :(得分:0)

getData()函数需要被调用,以便填充$ data。

 $cars = new MyClass();
 $cars->getData();
 echo $cars->showData('Honda');