Jump to content

Recommended Posts

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]):[];
}
?>


 

Link to comment
https://forums.phpfreaks.com/topic/300770-pass-post-value-or-post-name/
Share on other sites

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.

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.

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 by Jacques1
  • Like 1

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).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.