Existing columns vs. Null columns question
Existing columns vs. Null columns question
am 16.10.2007 15:46:07 von GameboyHippo
I have a get function that looks like this:
/**
* Magic function that gets properties
* @param mixed $var The property that should be retrieved
* @return mixed The value of the property. Returns null if property
doesn't exist
*/
function __get($var)
{
$special_properties = array('name');
if (!isset($this->data[$var]) && !in_array($var,$special_properties))
{
echo "Warning: $var is not a property for ".get_class($this)."!\n";
return null;
}
else{
switch ($var){
case 'name':
return $this->data['first_name'].' '.$this->data['last_name'];
break;
case 'office_phone':
case 'mobile_phone':
case 'other_phone':
return $this->to_phone_string($this->data[$var]);
break;
default:
return $this->data[$var];
}
}
}
Here's the question. Say I have a row in my database that has a null
value in the column other_phone. When it gets to the first if
statement !isset($this->data[$var]) it is going to evaluate to true
(in other words $this->data['office_phone'] is not set because it is
null). How do I evaluate if the column is merely not there instead of
not there or null?
Re: Existing columns vs. Null columns question
am 16.10.2007 16:11:18 von Captain Paralytic
On 16 Oct, 14:46, GameboyHippo wrote:
> I have a get function that looks like this:
>
> /**
> * Magic function that gets properties
> * @param mixed $var The property that should be retrieved
> * @return mixed The value of the property. Returns null if property
> doesn't exist
> */
> function __get($var)
> {
> $special_properties = array('name');
>
> if (!isset($this->data[$var]) && !in_array($var,$special_properties))
> {
> echo "Warning: $var is not a property for ".get_class($this)."!\n";
> return null;
> }
> else{
> switch ($var){
> case 'name':
> return $this->data['first_name'].' '.$this->data['last_name'];
> break;
> case 'office_phone':
> case 'mobile_phone':
> case 'other_phone':
> return $this->to_phone_string($this->data[$var]);
> break;
> default:
> return $this->data[$var];
> }
> }
> }
>
> Here's the question. Say I have a row in my database that has a null
> value in the column other_phone. When it gets to the first if
> statement !isset($this->data[$var]) it is going to evaluate to true
> (in other words $this->data['office_phone'] is not set because it is
> null). How do I evaluate if the column is merely not there instead of
> not there or null?
If $this->data['office_phone'] is set to NULL then I would expect !
isset($this->data[$var]) to evaluate to false, because !isset($this-
>data[$var]) IS set.
Re: Existing columns vs. Null columns question
am 16.10.2007 16:19:09 von luiheidsgoeroe
On Tue, 16 Oct 2007 15:46:07 +0200, GameboyHippo
wrote:
> How do I evaluate if the column is merely not there instead of
> not there or null?
http://nl2.php.net/manual/en/function.array-key-exists.php
--
Rik Wasmus
Re: Existing columns vs. Null columns question
am 16.10.2007 16:38:21 von GameboyHippo
On Oct 16, 9:11 am, Captain Paralytic wrote:
> On 16 Oct, 14:46, GameboyHippo wrote:
>
>
>
> > I have a get function that looks like this:
>
> > /**
> > * Magic function that gets properties
> > * @param mixed $var The property that should be retrieved
> > * @return mixed The value of the property. Returns null if property
> > doesn't exist
> > */
> > function __get($var)
> > {
> > $special_properties = array('name');
>
> > if (!isset($this->data[$var]) && !in_array($var,$special_properties))
> > {
> > echo "Warning: $var is not a property for ".get_class($this)."!\n";
> > return null;
> > }
> > else{
> > switch ($var){
> > case 'name':
> > return $this->data['first_name'].' '.$this->data['last_name'];
> > break;
> > case 'office_phone':
> > case 'mobile_phone':
> > case 'other_phone':
> > return $this->to_phone_string($this->data[$var]);
> > break;
> > default:
> > return $this->data[$var];
> > }
> > }
> > }
>
> > Here's the question. Say I have a row in my database that has a null
> > value in the column other_phone. When it gets to the first if
> > statement !isset($this->data[$var]) it is going to evaluate to true
> > (in other words $this->data['office_phone'] is not set because it is
> > null). How do I evaluate if the column is merely not there instead of
> > not there or null?
>
> If $this->data['office_phone'] is set to NULL then I would expect !
> isset($this->data[$var]) to evaluate to false, because !isset($this-
>
> >data[$var]) IS set.
I agree, however in practice, it doesn't work as expected. I'll check
out the other reply and see if that helps. Thanks!
Re: Existing columns vs. Null columns question
am 22.10.2007 16:50:09 von GameboyHippo
On Oct 16, 9:19 am, "Rik Wasmus" wrote:
> On Tue, 16 Oct 2007 15:46:07 +0200, GameboyHippo
>
> wrote:
> > How do I evaluate if the column is merely not there instead of
> > not there or null?
>
> http://nl2.php.net/manual/en/function.array-key-exists.php
> --
> Rik Wasmus
Sweet! array_key_exists works wonders! Thanks!