Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105342 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53764 invoked from network); 23 Apr 2019 08:06:48 -0000 Received: from unknown (HELO scarlet.netpirates.net) (188.94.27.5) by pb1.pair.com with SMTP; 23 Apr 2019 08:06:48 -0000 Received: from pd9e63fb4.dip0.t-ipconnect.de ([217.230.63.180] helo=[192.168.178.42]) by scarlet.netpirates.net with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1hIne6-00077h-L9 for internals@lists.php.net; Tue, 23 Apr 2019 07:07:06 +0200 To: internals@lists.php.net References: Reply-To: internals@lists.php.net Message-ID: <899de214-607e-9ad8-80cb-eeda5adf8ee6@php.net> Date: Tue, 23 Apr 2019 07:07:04 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Object Type Casting Reloaded From: sebastian@php.net (Sebastian Bergmann) Am 22.04.2019 um 23:47 schrieb Benjamin Morel: > 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, ...'); > } Today's equivalent is, at least for me, is this one-liner: assert($service instanceof EmailService); This way the IDE knows what type $service is supposed to be and there will be an exception at runtime (given the appropriate configuration) when this is not the case. Personally, I prefer hand-written factories that have an explicit createEmailService() method with a :EmailService return type declaration, for example, over the implicitness of a dependency injection container as the latter disguises and obscures dependencies.