I've read plenty about php OOP design principles and patterns. I can work with classes and inheritance. My issue is with actually using classes for some purpose. In this case, I want to create a simple api. Is my code below a reasonable approach to that? If not (and I imagine it isn't), what should I do instead?
$diC = new diC(); //dependency injection container
$router = $diC->get('router'); //parses the url, chooses the correct class/method to handle the reuqest
$format = $diC->get('format'); //compiles data into the requested media type
$data = $router->requestHandler();
/*instantiates a class based on the requested collection -- api/collection/item
then calls a method from that class based on the verb and whether the request is
for collection or item
*there is a default collection class that all collections extend
*each collection has 8 methods, GET, GET_item, POST, POST_item, etc
*/
$type = $router->getType(); //requested media type
$output = $format->$type($data); //format data to the media type
echo $output;
$format->$type($data);
is a tad error-prone. What if you want to use an array?$format->$arr['key']($data);
. There's an ambiguity issue lurking, perhaps consider using$format->{$type}($data);
for clarity. Though I must say, I don't care much for this kind of code... I can't see, at a moments notice, which method is being called. This makes the code less readable and harder to maintain – Elias Van Ootegem Oct 1 '13 at 11:13