Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77292 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96758 invoked from network); 17 Sep 2014 14:56:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Sep 2014 14:56:36 -0000 Authentication-Results: pb1.pair.com header.from=leight@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=leight@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.51 as permitted sender) X-PHP-List-Original-Sender: leight@gmail.com X-Host-Fingerprint: 74.125.82.51 mail-wg0-f51.google.com Received: from [74.125.82.51] ([74.125.82.51:55634] helo=mail-wg0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1A/36-57031-321A9145 for ; Wed, 17 Sep 2014 10:56:36 -0400 Received: by mail-wg0-f51.google.com with SMTP id k14so1512842wgh.34 for ; Wed, 17 Sep 2014 07:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=LLHCejkz5hFgZVVuEKMyHlI+lWbUprS856K/xBkEwio=; b=gCDG4iBGISoYVIWyLdNHAvyelreXLQv29yoQL4UqJUJqYz8j9Q6VZKlt+A9JjfwXXh ED6Yyx4U60mWd+3k63FihuMBeNDalXu/tfusFQUpUMViUQBgkb9xVc++XHSvccYd+SQX k7jOVwHP0D3jXQM40oTOZvnxx/kuMxpoiMdKuRI9+vCtfDOE1jIonmZT8ipxqR2aNorl 9nYkZXrMQp0ZAhNQSztlrTLoJUN8JLKAyJANTwv3aNgL56HuIH51bbyCVWPQtxEm2EY9 uIrPNlMFJ/SbUZDz7k9fQ6u8mDNLw1tTkQ0iSbUVFCPj96gmW2vsg9DvswxuYeFk1PmS EWCg== MIME-Version: 1.0 X-Received: by 10.180.206.230 with SMTP id lr6mr6244120wic.82.1410965792855; Wed, 17 Sep 2014 07:56:32 -0700 (PDT) Received: by 10.216.78.200 with HTTP; Wed, 17 Sep 2014 07:56:32 -0700 (PDT) In-Reply-To: <4ED7146272E04A47B986ED49E771E347BBF1BAF541@Ikarus.ameusgmbh.intern> References: <4ED7146272E04A47B986ED49E771E347BBF1BAF541@Ikarus.ameusgmbh.intern> Date: Wed, 17 Sep 2014 15:56:32 +0100 Message-ID: To: Christian Stoller Cc: "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Implementing interface method with child class in parameter def (Bug #42330) From: leight@gmail.com (Leigh) On 17 September 2014 15:52, Christian Stoller wrote: > Hello all, > > I hope the subject is not misleading. Please look at the following code: > > class A { } > > class B extends A { } > > interface C { > function foo(A $a); > } > > class D implements C { > public function foo(B $b) { > > } > } > > This code produces a "Fatal error: Declaration of D::foo() must be > compatible with C::foo(A $a) in /xyz/inheritance.php on line 10" > (see http://3v4l.org/l2M0f). > > I don't get the reason for that behavior (and I could not find any > documentation about that, at least not at > http://php.net/manual/en/language.oop5.typehinting.php). > > I have already found https://bugs.php.net/bug.php?id=42330 but Derick's > response does not help me and the linked file cannot be accessed > anymore. > > I'd say that it is absolutely legal to define a more specialized > type in a child or implementing class, or would this have any bad > side effects? > > Thanks and best regards > Christian Interesting that you bring this up today, I was literally talking to NikiC about this yesterday. This is what is called a covariant method argument type, and the reason that it is invalid is because if you pass an instance of D, to function that allows anything implementing C, then it would be acceptable to call foo() with an instance of A. The relevant article on wikipedia: https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Covariant_method_argument_type