note 100625 added to language.oop5.typehinting

From: Date: Wed, 27 Oct 2010 14:05:19 +0000
Subject: note 100625 added to language.oop5.typehinting
Groups: php.notes 
Request: Send a blank email to php-notes+get-173503@lists.php.net to get a copy of this message
Does PHP type hinting (with classes/interfaces) support covariant argument types? For example, if I want to define a generic "search" interface that takes a query and returns a result set, I might define: <?php interface Search { public function search(Query $query); // returns ResultList } interface Query { ... // various query criteria } ?> Then I want to specialise this to specific search services, e.g., a library search service might have: <?php interface BookSearch extends Search { public function search(BookQuery $query); } interface BookQuery extends Query { ... } ?> Here I want the query parameter to be covariant with the search service type. I can't use "self" as the query class is separate from the search class. In Java I'd use generics for this: public interface Search<Q extends Query> { public ResultList search(Q query); } public interface Query { //... } public class BookQuery implements Query { // ... Query plus specific parameters } public class BookSearch implements Search<BookQuery> { public ResultList search(BookQuery query); } Can I do it in PHP, or should I fall back to using untyped parameters? ---- Server IP: 69.147.83.197 Probable Submitter: 82.71.35.52 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php Edit -- https://master.php.net/note/edit/100625 Del: integrated -- https://master.php.net/note/delete/100625/integrated Del: useless -- https://master.php.net/note/delete/100625/useless Del: bad code -- https://master.php.net/note/delete/100625/bad+code Del: spam -- https://master.php.net/note/delete/100625/spam Del: non-english -- https://master.php.net/note/delete/100625/non-english Del: in docs -- https://master.php.net/note/delete/100625/in+docs Del: other reasons-- https://master.php.net/note/delete/100625 Reject -- https://master.php.net/note/reject/100625 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173503) next »