Pergunta

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.

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top