Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105352 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 50213 invoked from network); 23 Apr 2019 13:33:44 -0000 Received: from unknown (HELO mail-pf1-f171.google.com) (209.85.210.171) by pb1.pair.com with SMTP; 23 Apr 2019 13:33:44 -0000 Received: by mail-pf1-f171.google.com with SMTP id y13so7259200pfm.11 for ; Tue, 23 Apr 2019 03:34:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=7kN/Hf9LGywZAuH147YOyvwixpJ4agQ8g3EIslLKnNE=; b=pFgrEqx2Oe3GTN1w6DuXg8pdJKvFdq7NjiN40vBTFWuJltEv5sMdl65lavVAJ0jxyf PAAvoao3WF2UZEKLi2b6MQuhstCrxdapwf2YuhJnSYbLFar3P6FkE59ock6UXQLdMR15 7tm3ggjpTp3pDuDhH/GeId0LhAmlaZrFERsPHzPx7xK1hK/qQSqG6yCASmPg9PnZPCWv EzLBjIPVYeomu3aZEkap/1yCRv5ZuvlcKJ/f/TIPESKO3AfcUUQTUbb0H4SBW59wA23S 0BMN998dAJOL/tgz8zUFruFw1TjhfaXV1YkAFT/bvm8nTlLEp0OMsmMBVjwbPMblBUVE uGfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7kN/Hf9LGywZAuH147YOyvwixpJ4agQ8g3EIslLKnNE=; b=a9JxPF2LUag2NZFcJWsyCgGWaTNl6fcubTktyEFtIlCKqiwAX2dtp/1Pt6t1r70Yl/ zP1ulczqeqChD4X2cES2kzirl5d9HoY2WeDnbgW6hJNv5BGBgsLy7jMk+R3x+6yNdU+r a6362GktNA52zW2K4pfHeNxHJoag4XpOWe4+ZlBl4lION1fvs7qefA6GZslB0HT58aNA a+XSR3Wx+uCW8PcYvC/3a/gTReWh6u/8VjGpojvuNMhX7DYEYDj6HBfa9ioufOqRsS2q 2laBuzqR0G8AZDQK3qbEz+E/zz1kAeoLQvPo2Y9AYi/PpfMMkXcgLUyIu76rWwziu4HD h9pQ== X-Gm-Message-State: APjAAAUMFmzpXO+A5E7ATd6cnMPpsT7BEwTKsjg3zm6QInZ+4VXWCnSj dPO7Avp14uCYxZrs5MZMp8bvCKE08JT+MietH2LQXA== X-Google-Smtp-Source: APXvYqy/+kOXIFT3AbSF9VwBsr1HNK5lmO+f/jiVgNqdh4HXwzpGIj6aK7WFH9ByLUGHMQFNnejyyNcUKm0b/pOvj5s= X-Received: by 2002:a62:b418:: with SMTP id h24mr25444648pfn.145.1556015645573; Tue, 23 Apr 2019 03:34:05 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 23 Apr 2019 11:33:54 +0100 Message-ID: To: Benjamin Morel Cc: PHP Internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Object Type Casting Reloaded From: Danack@basereality.com (Dan Ackroyd) HI Benjamin, Similar to the nullable casting idea, you're taking a problem caused by your own code/framework, and then thinking it should be solved in core, when a simple solution is available in user-land. If you changed what you currently have: $service = $diContainer->get('email.service'); to also take the expected class: $service = $diContainer->get('email.service', EmailService::class); And then check inside your 'DI container' whether the expected type is returned, this solves the problem without needing new syntax. btw I'm sure you're already aware of it, but this is using a 'dependency injector' as a service locator. If your current DI library isn't powerful enough for you, rather than abusing it like this, I'd recommend looking at a different one, like https://github.com/rdlowrey/Auryn Also, similar: > > By the way, this RFC is a special case of something that could be far > more generic. If it was possible to register callbacks to be used when > casting, ... Apparently this might not be possible as it's ambiguous....which is a shame. cheers Dan Ack On Mon, 22 Apr 2019 at 22:47, Benjamin Morel wrote: > > Hi internals, > > I'd like to revive an old discussion about > object type casting. > > The idea would be to allow (ClassName) casting: > > $service = (EmailService) $diContainer->get('email.service'); > > The above code would throw a TypeError if the value is not an instance of > the given class. I see the following advantages: > > - Type safety: we can be sure that the value is of the correct type or that > we'll get an Error. This syntax allows to fail early if the variable > happens to not be of the expected type, and avoids much more verbose checks; > - Static analysis: IDEs and static code analysis tools can now understand > the type of the variable, without having to resort to `@var` annotations. > > These combine into a third advantage: readability. Today's equivalent of > the above one-liner could be: > > /** @var EmailService $service */ > $service = $diContainer->get('email.service'); > if (! $service instanceof EmailService) { > throw new TypeError('Expected instance of EmailService, ...'); > } > > Which is a lot of boilerplate code that could be easily avoided by > introducing this new syntax. > > Before moving forward and working on a formal RFC, I'd like to hear your > thoughts: what's your early feeling about this? Did I miss other > discussions around this subject? Are there any technical issues that come > to mind? Could this feature help the upcoming JIT compiler produce more > efficient machine code by knowing the type of the variable at compile time? > etc. > > Note: "casting" might not be the perfect name here as what we're really > doing is a type check, but this reuses the type casting syntax and > resembles Java's object casting. > > Thank you, > Ben