On Thu, 29 Jul 2004 14:41:57 +0200, Oliver Hitz <[EMAIL PROTECTED]> wrote:
> Hi all,
> 
> I have stumbled across something odd related to classes, instances and
> NULL in PHP 4. Apparently, an instance of a class that doesn't contain
> any variables is always equal to NULL.
> 
>   class MyClass {
>     function anyFunction() {
>       ...
>     }
>   }
> 
>   $c = new MyClass();
>   if ($c == null) {
>     print "is \$c really null?";
>   }
> 
> `is_null($c)' however, returns `false', as one would expect.
> 
> As soon as the class contains a variable, the `$c == null' comparison
> returns false.
> 
> Is there any logical reason why the comparison with the `==' operator
> returns `true'? I don't know about the internals of PHP, but I think
> this might be related to implementation details (e.g. instances of
> classes being associative arrays). However, from an OOP point of view
> this behaviour seems rather weird.
> 
> I may not be the first to notice this. I couldn't find anything in the
> mailing list, if there has already been a discussion about this, just
> point me to the right direction.
> 

Hmmm, I was about to say: "This is expected behavior", but the output
of this script I whipped up doesn't make sense to me:

class A {
}
$a = array(0,
'0',
'',
null,
array(),
new A());

echo '<table>';
foreach($a as $key => $val) {
  foreach($a as $key2 => $val2) {
    if($key != $key2) {
      echo '<tr><td>';
      var_dump($val);
      echo '</td><td>';
      var_dump($val2);
      echo '</td><td>';
      if($val == $val2) {
        echo 'yes';
      } else {
        echo 'no';
      }
      echo '</tr>';
    }
  }
}
echo '</table>';


I would sugest using === for anything critical where you don't
necessarily know the type.

-- 
DB_DataObject_FormBuilder - The database at your fingertips
http://pear.php.net/package/DB_DataObject_FormBuilder

paperCrane --Justin Patrin--

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to