php - Should a switch statement be used this way? -
is acceptable utilize switch
way of supporting multiple file formats, or bad practice?
like (untested):
<?php // load file switch ($data_format) { case 'xml': $openfile = simplexml_load_file($filename . '.xml'); break; case 'json': $openfile = json_decode(file_get_contents($filename . '.json')); break; } // stuff // save file switch ($data_format) { case 'xml': $openfile->asxml($filename . '.xml'); break; case 'json': file_put_contents($filename . '.json', json_encode($openfile, json_pretty_print)); break; } ?>
it depends on style of coding. if want work object oriented against idea. in case you'd utilize like:
class="lang-php prettyprint-override">$file = $repository->open($filename, $data_format); // stuff $file->save();
of course of study $repository->open()
needs homecoming object different save method depending on info format.
however if not working object oriented old procedural style cannot see wrong utilize of switch statement.
php switch-statement
No comments:
Post a Comment