Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108078 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 51603 invoked from network); 9 Jan 2020 22:56:59 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jan 2020 22:56:59 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 36E221804F4 for ; Thu, 9 Jan 2020 13:02:44 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 9 Jan 2020 13:02:43 -0800 (PST) Received: by mail-pf1-f175.google.com with SMTP id i23so6731pfo.2 for ; Thu, 09 Jan 2020 13:02:43 -0800 (PST) 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=c9W/a910qL2AYNET4y6/gUH2fPidDOfugAH4peg7ayE=; b=Z+EGFrCnQ/VjSS5DGgB3M4Gkv0W8OFCr8F9sO0ZY4IYf8WR+dPyaqPpKpTd1B1nIdv Qk2E33GkrdpdzCy42xYcM1zbHm8oGVKj6GaggBUbKag5F6d4e4QYpFgueXK+2d8ySOWB HKiHto0llbmz1Rh1ekqaW+TCz9T4zYkWERAmekR/kZBAnx4ju2TG6qr9H+0Z9vMcVjoJ moasZ3PDNJkF/J80ImFGTOQolsfU03TQIWEJ5I4dyL6jSc89MuyGeSmerFV7dCTdvcH6 zCavhnh29vPXreXZJ7KM/4lyJzcnha3TvRUgEU63OsgYreMgwMyqW5kjIzb5lLlDHLe8 Tzjg== X-Gm-Message-State: APjAAAVno7nuGkPx9sh6nrQm3jZGUxkyqOVALT28pQFNvAX9KOMzCuh0 EuriQZfYr0lQj5afboKCkbfBZecGckQUMuu15kc= X-Google-Smtp-Source: APXvYqz8frzU0G/s6pSfHD/zEdqc4u+bGRIIuFdVY5rMlC1iUnEJYd1LWg9XjQU2bD+Uy0GVB2IE6pkJIM3qHLJVlsE= X-Received: by 2002:a63:de47:: with SMTP id y7mr611085pgi.270.1578603762576; Thu, 09 Jan 2020 13:02:42 -0800 (PST) MIME-Version: 1.0 References: <13CC52AA-7690-42C6-89B7-B8FA4166BF38@newclarity.net> <57c08851-e6e2-c0bd-76e1-f7a0388d64b4@ralphschindler.com> <60610660-2E38-47BD-A998-1E226CEB3701@newclarity.net> In-Reply-To: <60610660-2E38-47BD-A998-1E226CEB3701@newclarity.net> Date: Thu, 9 Jan 2020 23:02:31 +0200 Message-ID: To: Mike Schinkel Cc: Ralph Schindler , Internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] Allow ::class on objects From: kalle@php.net (Kalle Sommer Nielsen) Moi Den tor. 9. jan. 2020 kl. 22.41 skrev Mike Schinkel : > > > On Jan 9, 2020, at 3:29 PM, Ralph Schindler wrote:How would you get the right semantics out of $object::interface, or $object::trait, and/or do you have an example of what you're expecting? > > Sorry, I was only thinking about using it on Interface and Trait names, not on objects. Traits are compiler assisted code copy/paste and not contracts (unlike interfaces), so there is no gain in having ::trait. > I have a lot of code that looks like this: > > use Interfaces; > if ( ! implements_interface( $object, Interfaces\MyInterface::class, $trigger_error = true )) { > return; > } > > And it feels wrong. I would love to be able to use ::interface, i.e.: > > use Interfaces; > if ( implements_interface( $object, Interfaces\MyInterface::interface, $trigger_error = true )) { > return; > } If your $object variable is an actual instance, you can use the instanceof operator, it treats the right operand as a first class citizen and allows you to skip writing ::class: use Interfaces; if(!$object instanceof Interfaces\MyInterface) { // Notice the ! is right associative and instanceof is non associative, hence the lack of parantheses } use Interfaces; if($object instanceof Interfaces\MyInterface) { } -- regards, Kalle Sommer Nielsen kalle@php.net