note 100625 deleted from language.oop5.typehinting by danbrown
| From: | danbrown@php.net | Date: | Wed, 27 Oct 2010 14:10:51 +0000 |
| Subject: | note 100625 deleted from language.oop5.typehinting by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173504@lists.php.net to get a copy of this message | ||
Note Submitter: nmadden
----
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?