note 49280 deleted from language.oop by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 19:03:39 +0000 |
| Subject: | note 49280 deleted from language.oop by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162564@lists.php.net to get a copy of this message | ||
Note Submitter: rdc_uk
----
Just found this interesting "gotcha";
If you implement a class with no member variables, calling the constructor does NOT produce an
object. This can be a source of (great) consternation when producing classes which need no data
storage.
Essentially if your class has no data members, all its methods are ONLY available statically, this
can be a pain if defining object hierarchies with abstract base classes...
Example:
class A
{
function A()
{
$i = 0; // not an empty constructor!
}
function fn()
{
return "A";
}
}
$obj_a = new A();
$obj_a == null
$obj_a->fn(); will generate an error
A::fn(); works
Simply putting a "var $status=0;" in the class suddenly makes your object instantiatable.
This is certainly a "quirk" in an OO implementation, possibly a bug, definitely worth
watching out for...