[PHP] Classes, instances and NULL

2004-07-29 Thread Oliver Hitz
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.

Thanks
Oliver


pgpCMvNTB17si.pgp
Description: PGP signature


Re: [PHP] Re: Classes, instances and NULL

2004-07-29 Thread Oliver Hitz
On 29 Jul 2004, Mehdi Achour wrote:
> Hi Oliver, you should test with === instead of ==
>   http://php.net/manual/en/language.operators.comparison.php

Thank you. I know there is a `===' operator, but to me this doesn't make
sense either.

  class A { }
  class B { var $x; }

It is logical that an instance of `A' is not identical to null. However,
why is an instance of `A' equal (`==' operator) to null, an instance of
`B' not? Do objects automatically evaluate to their associative array
representation? Is this intended behaviour?

Thanks
Oliver


pgpA5YT2eDqch.pgp
Description: PGP signature