PHP Classes: Call method in instance of a class by instance's name

Posted by Ursus Russus on Stack Overflow See other posts from Stack Overflow or by Ursus Russus
Published on 2010-03-28T23:56:47Z Indexed on 2010/03/29 0:03 UTC
Read the original article Hit count: 273

Filed under:
|

Hi, i have a class of this kind

Class Car {
private $color;

public function __construct($color){
$this->color=$color;
}

public function get_color(){
return $this->$color;
}

}

Then i create some instances of it:

$blue_car  = new car('blue');

$green_car = new car('green');

etc.

Now i need to call method get_color() on the fly, according to instance's name

$instance_name='green_car';

Is there any way to do it?

© Stack Overflow or respective owner

Related posts about php

Related posts about oop