note 68245 added to language.oop5.interfaces

From: Date: Wed, 19 Jul 2006 15:53:47 +0000
Subject: note 68245 added to language.oop5.interfaces
Groups: php.notes 
Request: Send a blank email to php-notes+get-114933@lists.php.net to get a copy of this message
Maybe this is a bit like cyrille.berliat's problem plus a somewhat solution. Let's say we want to create a queue class (or a stack, or a double linked list, the usual... :-). Wouldn't it be nice if we could define a generic interface or abstract (base) class that dictates the way specific queues have to look like ? Without having to specify the type of data to be queued. But we _do_ want to be strict about the type of data to be passed to a specific queue type. In other words: a bit of generic programming. Alas, it seems that Type Hinting and Interface/abstract don't get along :( But there might be a more runtime-involved solution. See below. So, this won't work: ------ abstract class Object {} // as PHP(5) doesn't have a super-Object-base-class class Cdata extends Object { } // i.e. Cdata IS-A Object interface Iqueue { public function enqueue(Object $data); } class Cmyqueue implements Iqueue { public function enqueue(Cdata $data) { } } $q = new Cmyqueue; $data = new Cdata; $q->enqueue($data); ------ Nor will this 'compile': ------ abstract class Object {} class Cdata extends Object {} abstract class Iqueue extends Object { abstract public function enqueue(Object $data); } class Cmyqueue extends Iqueue { public function enqueue(Cdata $data) { } } $q = new Cmyqueue; $data = new Cdata; $q->enqueue($data); --------- This however is possible: abstract class Object {} class Cdata extends Object { function __toString() { return('data'); } } abstract class Iqueue extends Object { abstract public function enqueue(Object $data); } class Cmyqueue extends Iqueue { public function enqueue(Object $data) { echo "enqueue '".$data->__toString()."'\n"; } } or: interface Iqueue { public function enqueue(Object $data); } class Cmyqueue implements Iqueue { .... } We could do our special Cmyqueue specific pre-processing before enqueu()ing. But the compiler won't warn us if we pass anything other than the object we expect to be passed. We'd have to do something runtime like this: class Cmyqueue extends Iqueue { private function checkObjectType(Object &$obj) { if (get_class($obj) !== 'Cdata') throw new Exception("You can only pass CData's to Cmyqueue"); } public function enqueue(Object $data) { $this->checkObjectType($data); echo "enqueue '".$data->__toString()."'\n"; } } hope this clears up some... ---- Server IP: 66.163.161.117 Probable Submitter: 212.203.31.34 (proxied: unknown) ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.interfaces.php Edit -- http://master.php.net/note/edit/68245 Del: integrated -- http://master.php.net/note/delete/68245/integrated Del: useless -- http://master.php.net/note/delete/68245/useless Del: bad code -- http://master.php.net/note/delete/68245/bad+code Del: spam -- http://master.php.net/note/delete/68245/spam Del: non-english -- http://master.php.net/note/delete/68245/non-english Del: in docs -- http://master.php.net/note/delete/68245/in+docs Del: other reasons-- http://master.php.net/note/delete/68245 Reject -- http://master.php.net/note/reject/68245 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#114933) next »