Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85660 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64375 invoked from network); 1 Apr 2015 19:25:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Apr 2015 19:25:19 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.49 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.215.49 mail-la0-f49.google.com Received: from [209.85.215.49] ([209.85.215.49:33597] helo=mail-la0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 74/38-21906-E164C155 for ; Wed, 01 Apr 2015 14:25:18 -0500 Received: by lajy8 with SMTP id y8so44354790laj.0 for ; Wed, 01 Apr 2015 12:25:15 -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=tXqgSs8w45vPLuAKPf1Uz+MiP80AgflEFXQtOk2RjhQ=; b=iNCgUQFdVR/Bxjp8eg5Iuh+LMya/+CrGyf3/aalucSoU1o3TwrixyrZkTvtH6Fixno t6LaNL0YZnkt/mxK0IwWrH2grP1PQQbuvT7tm/bExi333bPqpOuQHt8FeSHjbpWZOUUX IOyzpoVYEDm1LPEsTkYcDmwBWUiI9GUDhngoMlTakcoUHhRoG041gbfteOB0rNDWtPfe Iu8mJEhuo63DoMzSMNMnRVBzVuxOPQAcUx6EZ5lo4u0t6ZJ/dH/G2fdnDpW+cdLAd5hC u+ew+fT1NpvDPm3Pa4bJzsya9MAdYRVaP4JsMiByWq/RhrHYREo4JDFt6KVk82jzzFR/ A9CQ== MIME-Version: 1.0 X-Received: by 10.112.171.65 with SMTP id as1mr37347442lbc.45.1427916315357; Wed, 01 Apr 2015 12:25:15 -0700 (PDT) Received: by 10.25.144.15 with HTTP; Wed, 1 Apr 2015 12:25:15 -0700 (PDT) In-Reply-To: <551C3CF5.5060704@mabe.berlin> References: <551C3CF5.5060704@mabe.berlin> Date: Wed, 1 Apr 2015 15:25:15 -0400 Message-ID: To: Marc Bennewitz Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Overwrite return type-hint into a more specific one From: ircmaxell@gmail.com (Anthony Ferrara) Marc, On Wed, Apr 1, 2015 at 2:46 PM, Marc Bennewitz wrote: > Hi internals, > > On experimenting with return type-hints I noted an inconsistency on > overwriting an existing hint to a more specific one. On adding a non > existing return type-hint on overwrite it's fine but it's logically the same > (this previously was an E_STRICT). > > http://3v4l.org/YdB8s - works fine (E_STRICT lower then php7@20150401) > http://3v4l.org/UDk0n - Fatal error: Declaration of Bar::setTest() must be > compatible with Foo::setTest($test): Foo > http://3v4l.org/AuOsr - HHVM works fine That's the concept of Variance: https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science) Specifically, that return types can be covariant (subtypes can "narrow" return types) and parameters can be contravariant (subtypes can "widen" paramters). So this is valid: class A extends B {} class C { public function foo(A $b): B {} } class D extends C { public function foo(B $b): A {} } In that everything that C::foo accepts, D::foo accepts, but D::foo can accept more. And D produces more strictly (in that anything that can work with a product of C::foo can work with what D::foo produces). We looked into doing this for return types (and all other declarations). However, we ran into some non-trivial problems around compilation. Currently, types are never loaded from a declaration. They are only loaded when instantiated. Therefore, it's completely possible to have a declared class with undeclared type declarations. If the type declaration remains undeclared when you call the method, it's by definition a failed type (since `$obj instanceof UndefinedClass` is always false). If we want to check variance, we need to know about the type at compile time. This brings up an issue that basically makes it impossible to separate code into multiple files without an autoloader. For example: