NotionCommotion Posted February 9, 2016 Share Posted February 9, 2016 Please comment on the pros and cons of the two approaches. Thank you <?php $stuff1=isset($_POST['stuff'])?getStuff1($_POST['stuff']):[]; function getStuff1($value){ return bla($value); } $stuff2=getStuff2('stuff'); function getStuff2($name){ return isset($_POST[$name])?bla($_POST[$name]):[]; } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 9, 2016 Share Posted February 9, 2016 What does 'bla' do for you when you already have the POST value in hand? And what is '[]' supposed to represent? Plus your functions are not consistent. You pass in a name simply to use it in POST but then you hardcode a POST[index] in the function. If you are going to pass something to a function, keep your function generalized so that it may be used universally. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted February 9, 2016 Author Share Posted February 9, 2016 What does 'bla' do for you when you already have the POST value in hand? And what is '[]' supposed to represent? Plus your functions are not consistent. "bla" does something, and is not relevant to the question. "[]" is an array (you might want to look into it). I believe the functions are consistent. If you are going to pass something to a function, keep your function generalized so that it may be used universally. Thank you. This is very relevant. So, you recommend the first as it is more universal. Good point. The second, however, reduces content of code assuming the function is used often. Note sure if it is worth it, and thus why I am asking the question. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 9, 2016 Share Posted February 9, 2016 (edited) Hard-coded $_POST references in the application logic are bad, because they tie your entire code to specific details of the HTTP protocol. Actually, it's even worse: You're tied to the low-level superglobals of vanilla PHP. What if you switch to a framework which has a different HTTP interface? What if you want to reuse the code in a different context? You cannot -- unless you manually go through your code and replace the superglobal mess. Stuff like $_POST should only exist in the code which takes care of the HTTP details. Outside of that, you should pass values around and not make any assumptions about their origin. Edited February 9, 2016 by Jacques1 1 Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted February 9, 2016 Author Share Posted February 9, 2016 Thanks Jacques, If I wasn't already convinced, I definitely am now. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 9, 2016 Share Posted February 9, 2016 Your use of bla() inside of the getstuff function is kind of strange. Why not just use bla in your mainline code and eliminate the need for getstuff1()? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted February 9, 2016 Author Share Posted February 9, 2016 Hi Ginerjm, bla() was just an example. Instead of a function, assume it was a bunch of script which I didn't wish to duplicate in getstuff(). Did you look into []? Just a shortcut for array(), and [1,2,3] is the same as array(1,2,3). Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 9, 2016 Share Posted February 9, 2016 I thought of that originally but then I thought "why would anyone want to return an empty array as a result of a POST value?" and said it must mean something else. IMHO I would just return a False like so many PHP functions do Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.