PHP Classes

Your class does not work as expected.

Recommend this page to a friend!

      IsSetAnd  >  All threads  >  Your class does not work as expected.  >  (Un) Subscribe thread alerts  
Subject:Your class does not work as expected.
Summary:Package rating comment
Messages:4
Author:Artur Graniszewski
Date:2011-02-25 09:09:50
Update:2012-12-24 03:49:23
 

Artur Graniszewski rated this package as follows:

Utility: Insufficient
Consistency: Good
Examples: Good

  1. Your class does not work as expected.   Reply   Report abuse  
Picture of Artur Graniszewski Artur Graniszewski - 2011-02-25 09:09:50
Your class does not work as expected. You shouldn't use isset() function to check if the given array key is set.

Let me give you an example:

$array = array("imSet" => null, "imSetAlso" => "yes");

var_dump(isset($array["imSet"])); // returns false!!!
var_dump(isset($array["imSetAlso"])); // returns true!!!

So basically you should use array_key_exists() function:

var_dump(array_key_exists("imSet", $array)); // returns true

  2. Re: Your class does not work as expected.   Reply   Report abuse  
Picture of David Hyland David Hyland - 2011-03-03 16:21:54 - In reply to message 1 from Artur Graniszewski
dude's got a point. otherwise very nice class indeed. thanks for sharing.

  3. Re: Your class does not work as expected.   Reply   Report abuse  
Picture of Corey Tisdale Corey Tisdale - 2012-12-24 03:34:07 - In reply to message 1 from Artur Graniszewski
Conceptually you are correct, but the fix requires changing the name of the class to KeyExistsAnd. I think maybe the class as a whole should not exist. In any event, good point, sorry I didn't think of this before.

  4. Re: Your class does not work as expected.   Reply   Report abuse  
Picture of Corey Tisdale Corey Tisdale - 2012-12-24 03:49:23 - In reply to message 1 from Artur Graniszewski
Actually, now that I am thinking about it, there is no way to return a value that is always appropriate. Essentially, since the class stores null value to signify that your value didn't exist, it invalidates the test for a null value. By this I mean if you test for a value that does exist, but is null (also, it is safe to say null values do not exist - I think this is the point of null values), you get null. If you test for a value that does not exist, you get null. There is no way to design a class with similar functionality that allows for different results between null valued array keys and non-null valued array keys.

That being said, the method by which I test is flawed, and I should be using array_key_exists. I think the class name should still be IsSetAnd, though, because the actual results you get are similar to those you get from isset, namely that having an explicitly declared null value is equivalent to having no explicitly declared value.

In any event, thank you for making me a better coder.