TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: PHP, interfaces, typehinting subtype and LSP?

2 pointsby enterxover 11 years ago
interface I<p>{<p>function foo(stdClass $arg);<p>}<p>class Test extends stdClass<p>{<p>}<p>class Implementation implements I<p>{<p>function foo(Test $arg)<p>{<p>}<p>}<p>Result:<p>Fatal error: Declaration of InterfaceImplementation::foo() must be compatible with I::foo(stdClass $arg) in test.php on line XY<p>How come that I can&#x27;t type hint a subtype in the implementation?

2 comments

jaachanover 11 years ago
It has to work with all subtypes of stdClass, since in order for the interface to hold its word, I need to be able to get an object thats implements I, and pass it any stdClass, regardless of what they do with it.
评论 #6835052 未加载
FreezerburnVover 11 years ago
This is a question that would be better suited to asking on stackoverflow.com
评论 #6834274 未加载