Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 3.0 Proposed Change: NULL v FALSE
#31

[eluser]joelkallman[/eluser]
Quote:If something is set then it should return whatever value is there, if something is not set then it should be NULL.

I agree. This is the correct way to do it.

So now onto a few more questions. Phil mentioned the following methods that would be affected, but I believe there should be more if we are to be consistent.

Code:
$this->input->get();
$this->input->get_post();
$this->input->post();
$this->input->cookie();
$this->input->server();
$this->input->post();

$this->session->userdata();
$this->session->flashdata();

// New ------------------
$this->uri->segment();
$this->uri->rsegment();

$this->config->item();

$this->lang->line();

Also, all the related helper functions need to be documented as well.

A few more questions on functionality. Since we added the ability for $this->input->post() to return the full $_POST array when called, this should return an empty array when there is nothing in $_POST. This is how default PHP functions and should also apply to get() and cookie().

So to be clear, assuming $_POST === array(), the following code should operate as follows.

Code:
$foo = $this->input->post('foo');
$bar = $this->input->post();

var_dump($foo); // OUTPUTS: NULL
print_r($bar); // OUTPUTS: Array()


Anyway, those are some of my thoughts. What do you think?
#32

[eluser]Phil Sturgeon[/eluser]
As I have said please do not post random feature requests in the proposal thread, as it confuses the matter that we are talking about. Array returns are not being discussed here.
#33

[eluser]toopay[/eluser]
+1
#34

[eluser]joelkallman[/eluser]
[quote author="Phil Sturgeon" date="1335283676"]As I have said please do not post random feature requests in the proposal thread, as it confuses the matter that we are talking about. Array returns are not being discussed here.[/quote]

It is not a random post or a feature request. If we are talking about expected behavior, let's be consistent. (That is what we are talking about, right?)

Right now you are suggesting a change from FALSE to NULL (which is correct behavior). Why would we then disregard the other parts of the system that would expect the same behavior. Right now the system IS consistent, even if it is "wrong". You are talking about making CodeIgniter consistent with PHP behavior. I'm all for that, but let's be consistent if we are going to make a change.

As far as returning an array, why would you return NULL when you ask for the full $_POST array? That makes just about as much sense as returning FALSE when requesting a specific key. If we want to be consistent with PHP behavior, let's be consistent. Otherwise, let's frame this discussion in some other context.
#35

[eluser]Phil Sturgeon[/eluser]
[quote author="joelkallman" date="1335287734"][quote author="Phil Sturgeon" date="1335283676"]As I have said please do not post random feature requests in the proposal thread, as it confuses the matter that we are talking about. Array returns are not being discussed here.[/quote]

It is not a random post or a feature request. If we are talking about expected behavior, let's be consistent. (That is what we are talking about, right?)[/quote]

Obviously. I am not surprised there are some methods I have not listed and I'm sure you've missed a few too. If this gets the go ahead I will painstakingly go through finding every single method in every single library. Obviously I am not going to half-arse it. These few provide enough of an example for us to talk things through.

[quote author="joelkallman" date="1335287734"]As far as returning an array...[/quote]

That is the part where you are going off track and randomly talking about arrays for some reason.

Right now the code looks like this:

Code:
protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
{
  if ( ! isset($array[$index]))
  {
   return FALSE;
  }

  if ($xss_clean === TRUE)
  {
   return $this->security->xss_clean($array[$index]);
  }

  return $array[$index];
}

I plan to make it look like this:

Code:
protected function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
{
  if ( ! isset($array[$index]))
  {
   return NULL;
  }

  if ($xss_clean === TRUE)
  {
   return $this->security->xss_clean($array[$index]);
  }

  return $array[$index];
}

That one difference is pretty much the scope of this conversation and I would like to keep it to that.
#36

[eluser]joelkallman[/eluser]
[quote author="Phil Sturgeon" date="1335288445"]Obviously I am not going to half-arse it. [/quote]

Agreed. You and the Reactor Engineers have proven that.

[quote author="Phil Sturgeon" date="1335288445"][quote author="joelkallman" date="1335287734"]As far as returning an array...[/quote]

That is the part where you are going off track and randomly talking about arrays for some reason.
[/quote]


Let me see if I can explain a little clearer.

Code:
// When called without a 'key' passed, this will always return the full array if one exists.
$this->input->post();

// Consistent behavior from PHP to CodeIgniter
// This is what is being proposed in order to maintain consistency
$_POST['key'] === $this->input->post('key') === NULL; // Assuming the key doesn't exist.
$_POST === $this->input->post() === array(); // When no POST data exists. - This is what I am adding to your proposal

// Example code as it exists right now
$post_data = $this->input->post() ? $this->input->post() : array();

foreach ($post_data as $key => $val)
{
  // ... Do something
}

// Expected behavior if it follows PHP
foreach ($this->input->post() as $key => $val)
{
  // ... Do something
}


I don't know if that makes it any clearer. I want to be able to use the $this->input->post() method in the exact way I would expect to use the $_POST superglobal. If there is a key that doesn't exist, then it returns NULL. If accessing it without a key, it always returns an array, whether it is empty or not. Since we are proposing a change, I don't want to go half way to expected behavior because then we would still be stuck checking the returned value from $this->input->post() to see if it is_array() or something like that.

<b>I understand that you want to keep the scope to just returning NULL as opposed to FALSE, but I feel that scope is too limited in order to justify the change you are proposing. IMHO we should go all the way or stay where we are.</b>

I agree with your code adjustment, if we also make the following change of the code from this:

Code:
function post($index = NULL, $xss_clean = FALSE)
{
// Check if a field has been provided
if ($index === NULL AND ! empty($_POST))
{
  $post = array();

  // Loop through the full _POST array and return it
  foreach (array_keys($_POST) as $key)
  {
   $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
  }
  return $post;
}

return $this->_fetch_from_array($_POST, $index, $xss_clean);
}

... to this...

Code:
function post($index = NULL, $xss_clean = FALSE)
{
// Check if a field has been provided
if ($index === NULL)
{
  $post = array();

  // Loop through the full _POST array and return it
  foreach (array_keys($_POST) as $key)
  {
   $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
  }
  return $post;
}

return $this->_fetch_from_array($_POST, $index, $xss_clean);
}
#37

[eluser]Phil Sturgeon[/eluser]
I disagree. If you try to access $this->input->post() and you have not actually send ANY post data at all then it should be NULL as there is no post data at all, we don't even know that it should have been an array because there isnt anything. If the job of every method was to exactly emulate native PHP features (stupid bits and all) then there would not be much point to any of it, would there.

I wont ask again, please don't distract the original conversation. This is a proposed change that discusses changing logic existing in 2.x for 3.x. If you are unhappy with the way that $this->input->post() works then crack on and make a pull request as it has nothing to do with this topic.
#38

[eluser]joelkallman[/eluser]
[quote author="Phil Sturgeon" date="1335291252"]If the job of every method was to exactly emulate native PHP features (stupid bits and all) then there would not be much point to any of it, would there.[/quote]

True.

[quote author="Phil Sturgeon" date="1335291252"]I wont ask again, please don't distract the original conversation. This is a proposed change that discusses changing logic existing in 2.x for 3.x.[/quote]

No problem! For me the scope of NULL vs FALSE is too limiting to agree with the change. I was assuming this was open for discussion and not just a YES or NO answer. Carry on everybody! Smile
#39

[eluser]Unknown[/eluser]
+1 from me as well

and not to veer off topic again but joelkallman I kinda get what you were going for but it seems like that would only make consistent behaviour between PHP <-> Codeigniter but not within Codeigniter itself, which is the main issue I think.
#40

[eluser]skunkbad[/eluser]
I think NULL instead of FALSE is fine. For smaller existing applications, I don't think it's that hard to go through and fix little problems, and even for medium sized applications, I expect to do some work when upgrading. I do work daily on a massive application and there's no way we would upgrade that to 3.0 anyway ( Boss doesn't care for the new license ).




Theme © iAndrew 2016 - Forum software by © MyBB