php - Should POST array be dependency-injected or can it be just read from anywhere? -
is there reason dependency-inject post array controller?
for example, have this:
class controller { function __construct() { $this->coverpagechoice = $_post['coverpage']; $this->printnameplates = $_post['print_nameplates']; $this->commentsonseparatepage = $_post['comments_on_new_page']; } }
is there benefit injecting $_post array constructor, this:
class controller { function __construct(array $input) { $this->coverpagechoice = $input['coverpage']; $this->printnameplates = $input['print_nameplates']; $this->commentsonseparatepage = $input['comments_on_new_page']; } }
sure, second better, example improve testability. , avoid dependency outer world (yes, php itself), so, improves encapsulation.
its better abstract $_post out sort of class, have consistent access it, example request in symfony: https://symfony.com/doc/current/components/http_foundation.html#request
Comments
Post a Comment