문제

I have the following objects which I pass to a function:

$this->cars->car1->engine='v8';
$this->cars->car1->hp='330';

$this->cars->car2->engine='v6'
$this->cars->car1->hp='210';

handle_car($this->cars->car1);
handle_car(this->cars->car2);

The function handle_car needs to find out what car it is ie whether its "car1" or "car2".

How can I go through the property keys to see if the property is car1 or car2? I have tried using the get_object_vars function but it only returns the keys of 'car1' and 'car2' not those itself.

도움이 되었습니까?

해결책

You are passing property values to your function, not property names. You should pass name directly to determine it inside function properly. Remember, $this->cars->car1 or $this->cars->car2 are just objects, where could be anything.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top