Just as an alternative: if you're using objects for all your data types, and you want to deal with just a single parameter in your method call, you could assign the name of the variable based on the type of object received.
class View {
public function make($object) {
//if it's an object - use the type of object to define the variable
if(is_object($object)) {
switch(get_class($object)) {
case 'Movie':
$movie = $object;
break;
case 'CD'
$cd = $object;
break;
// ... and so on
default:
$data = $object;
break;
}
}
//otherwise go with a default of $data
else {
$data = $object;
}
}
}
I'd personally feel the need to validate the variable with isset()
in every view file though, just in case the wrong type of object was passed in - and I fear it would end up with far more validation required in all the view files than necessary.
To go one step further
Alternatively your classes could all extend a common (abstract) class which can hold the type - set that type in the constructor of all your sub-classes and determine the variable name based upon that (set the parent's protected $type;
with $this->type = 'movie';
inside the Movie
constructor for instance).
This would give you something like this (note: ${$object->getType()} = $object;
) which works but I'm not sure about the sanity of it:
class View {
public function make($object) {
//if it's an object - use the type of object to define the variable
// so for the `Movie` object where the type is 'movie' this will
// create a variable called $movie containing the Movie object
if(is_object($object) && method_exists($object, 'getType')) {
${$object->getType()} = $object;
require_once "/path/to/{$object->getType()}.php";
}
}
}
/**
* base abstract 'thing'
*/
abstract class Thing {
protected $type;
public function getType() {
return $this->type;
}
}
/**
* a Movie 'thing'
*/
class Movie extends Thing {
public function __construct() {
$this->type = 'movie';
}
}
//processing stuff
$oView = new View();
$oView->make( new Movie() );