28/08/08
Good OOP Practice / Zend framework
An interesting post on nabble yesterday by Matthew Weier O’Phinney on the difference between:
if ($this->getRequest()->isPost()) { // process } if ($this->_request->isPost()) { // process } if ($_SERVER['REQUEST_METHOD'] == 'POST') { // process }
Sometimes, he explains
“the obvious and simple solutions simply are not portable, or would circumvent custom logic the developer may need to utilize.”
You could use $this->_request
“However, if you ever modify getRequest() in your class or in a custom base controller class, then you may be accessing the wrong property or overriding necessary business logic. For this reason, we recommend using getRequest() to grab the request object.”
“Next, using isPost() is more portable than using $_SERVER['REQUEST_METHOD']. The reasons are that your web server may or may not populate this environment variable, and for testing. With testing, we allow you to specifically set the request method — $_SERVER is never modified in this case. This gives you the ability to test your applications without needing a web server involved.”