Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96586 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33772 invoked from network); 24 Oct 2016 08:27:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Oct 2016 08:27:18 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.43 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.43 mail-wm0-f43.google.com Received: from [74.125.82.43] ([74.125.82.43:34374] helo=mail-wm0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 96/DA-28528-1E5CD085 for ; Mon, 24 Oct 2016 04:27:15 -0400 Received: by mail-wm0-f43.google.com with SMTP id f193so14058576wmg.1 for ; Mon, 24 Oct 2016 01:27:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=MD3C4lT4xqq02MZqV/NmYeARpScIc4AP1CQKYmz4IfM=; b=pvdEIFbjWun5Blu1S+qP5RdYCRsAro+yIzjb9IUHTMdGWAqr4TZQki9GopFiMOAqGK yE0iIgidIrvgDWZ/qu145cO2JLShLu6bF3V8VmK0M9udIvmBpwsNBJhrQK9nSNPs2r/G QpA99w34ivahxcCYrj+ua6qiGcH++2BscU0rzvhLE2bvgPf0oT58N1aauev7JxPnMHTK dXueqfRpb7xaRzOJ/FoKIhBE1FLWc84+64hlcJSYuA6+/NHx4PInhlQk8PI4mkTEG/QX 75nirJMe+oFXF3wYMNu4vzg/GdO0ZDEVXIjyGq9toy/S1GTT/wkMXBGGkgVORQj/Y/xW OHCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=MD3C4lT4xqq02MZqV/NmYeARpScIc4AP1CQKYmz4IfM=; b=IVcHbVPcJmdSc6fuBuPy7WgseAzh/yYwuCnPJFMA2JypyuLbjGe653vZZP1loY8a2l p7EMaPIU/9cC5o3esJ92/cwa9d38zPmB4S11jPPYfoo04L3TQxVqU4YcCfybnN2o8XvB zq709kBu3ANL2vY8ZYWZ/SC0sScSZt1p9uakAyh70bqXUauvpIxh8jCsCvYzRzQkzRJ1 kagYKawNDgVOd6r2wP/HZj7+q2ViFZwfzIIwwVHpD2Szo7BWm4AvaTlKXJyiqkdz466m KgrFFVj5l5PyuAjVP3zP3GeKgqylxoJsEglnB/dkKWamDVQE6hAVA78DiFRxiyUimpOl pFdA== X-Gm-Message-State: AA6/9Rm94+0CbxyGpraa+Ko13a7Z7rIjKVstmQ7umaaloFxKg/JfmEFt57AeAVcD8R/ovIt6PshrCcFf3cB0zQ== X-Received: by 10.28.142.82 with SMTP id q79mr13030957wmd.20.1477297631235; Mon, 24 Oct 2016 01:27:11 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.109.170 with HTTP; Mon, 24 Oct 2016 01:26:50 -0700 (PDT) In-Reply-To: References: Date: Mon, 24 Oct 2016 01:26:50 -0700 Message-ID: To: =?UTF-8?Q?Micha=C5=82_Brzuchalski?= Cc: PHP Internals List Content-Type: multipart/alternative; boundary=001a114426fc899368053f9828a9 Subject: Re: [PHP-DEV] [RFC][DISCUSSION] Object type hint From: ocramius@gmail.com (Marco Pivetta) --001a114426fc899368053f9828a9 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Michal! First of all, many thanks to you and Dan for pushing this forward. It is indeed a very needed and long due addition to the engine. I just wanted to clear up the use-case scenarios for everyone in here that keeps stating that this will lead to bad code. It is very unlikely to type-hint "object" inside your business domain. Where this functionality is useful is any of the following (with examples): - generic hydrators/property access (these exist, and they are widely used inside form components) class SillyLibraryHidrator { public function hydrate(object $object, array $someData) { // pseudo: foreach ($objectReflectionProperties as $p) { $p->setValue($object, $someData[$p->getName()]); } } } - serializers - also widely used, can generally tell them to produce an object of a certain type. Consumers would then wrap it, and use a more specific type-hint interface JsonSerializer { public function serialize(object $object) : string; public function deSerialize(string $data, string $className) : object; } - identity maps - typically part of ORMs interface IdentityMap { public function add(string $id, object $object) : void; public function getId(object $object) : ?string; public function getObject(string $id) : ?object; } - code generators interface WrapThingIntoAProxy { public function wrap(object $object) : object; } - instantiation logic for generic factories interface AGenericFactory { public function create(array $data, string $className) : object; } - table data gateways/repositories interface TableDataGateway { public function fetchById(array $identifier) : ?object; } These are just some of the examples that get direct benefit from this sort of type-hint, or at least the scenarios that I work with on a daily basis. Please don't tell me that I have to get back to `is_object()`, because: * it is implicitly part of the implementation, and not part of the reflectable signature *AS IT SHOULD BE* * it is more code for me to test * I cannot throw a TypeError myself, if a non-object is given (TypeError can't be instantiated in userland, AFAIK), and every time I have to design custom exceptions (also to be tested/documented) * this way is actually faster than a manual `is_object()` check * intent is conveyed to the consumer directly, and not via documentation docblock * allows for explicit nullability, yet splitting object/non-object APIs * many library-level codebases rely on generic code that doesn't know about one specific type, but just about "objects" The alternative is going with full generics, but that's not viable for now. This additional type hint is pretty much covering the scenarios above in a very approachable way. Greets, Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On Sun, Oct 23, 2016 at 12:39 AM, Micha=C5=82 Brzuchalski wrote: > Hi all, > > I would like to initiate discussion for Object typehint RFC > https://wiki.php.net/rfc/object-typehint > > This feature is developed to provide missing functionality which is neede= d > and quite easy to introduce. > There are many people which I've talked about the benefits of this > functionality. > > For those who doesn't like the idea and think they won't need neither use > it rather than just saying 'no' they > please say why other people who want to use that parameter type are wrong= , > for wanting to do that. > > If there is anything left to discuss, please comment. > > Thanks to @Danack for drafting. > -- > regards / pozdrawiam, > -- > Micha=C5=82 Brzuchalski > about.me/brzuchal > brzuchalski.com > --001a114426fc899368053f9828a9--