質問

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