note 49280 added to language.oop

From: Date: Sat, 22 Jan 2005 16:44:40 +0000
Subject: note 49280 added to language.oop
Groups: php.notes 
Request: Send a blank email to php-notes+get-83784@lists.php.net to get a copy of this message
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... ---- Manual Page -- http://www.php.net/manual/en/language.oop.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49280 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49280&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49280&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49280&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49280&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49280&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49280&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#83784) next »