Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104244 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 66429 invoked from network); 6 Feb 2019 16:55:33 -0000 Received: from unknown (HELO mail-it1-f178.google.com) (209.85.166.178) by pb1.pair.com with SMTP; 6 Feb 2019 16:55:33 -0000 Received: by mail-it1-f178.google.com with SMTP id z20so6390761itc.3 for ; Wed, 06 Feb 2019 05:36:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=fpi+2L+3XXI9mXoSrcebq41BmhNd51T8iTcC3vUFN3U=; b=YjMD/YbhOgkMD7s+le7h+7Pf3PlgahzB5A1G9JBWyQLY47tPXjdy9FN0OxafeB9TrG nU0pJQZl56sd9MgVVZ1u/O/GE9U21IclYoAPJCzx8wCJARe+6PqhJ7H8m+qpdpYpXKMo a/JXa5lYw99vXxAQpjtpecYkJg+ZswK7D3z9BMKTz6y/lXhNmJNQ4LjemplwDYl3PBHo P6X2ekfEScLg+b7WZMt+ya3Gc/wyuYiUBM8y7k7qcditzbhogpl7Zu6WqOZvytClW3KV tfc+KIMtEFAZbbb6rdKZy0RkASGRqOXBYNlu1sA3lHPayHC5oUbB1zTzyl3ftEQOmDBv s+YA== 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=fpi+2L+3XXI9mXoSrcebq41BmhNd51T8iTcC3vUFN3U=; b=Do4alcA1WIEX0VFQDIu4c0+C1IyhVOsD1CWpM36QuTR7p5UB5QUuOQKhulnCFN9GMa mPi9FyIqLkjrAC1ztS4xVAYUqUwHF3qxk5wBWEgzoi2Z4f2KxiakJGrD4D3KLqI972M4 ziHmlqIyHYh06m4HJF9vcaM+gq4B92ANcAZMSCP+LXP/NNPuEgsCK27soS3eBWenLpFS HH0bnO2x7iQU1c/eFYcw+bMWqT2ZKJjQoKzIRbghwuMn8qHpXb5tKdkcQgbU3+X31g2B 80MPtcc4oYAx51+DqFYlLSy+rzZQESy4zwbqV684mPl0w6hQXJ11RSJlBM46UPYUCssc tYIw== X-Gm-Message-State: AHQUAua7tPf84D9YYVZyhza2sKWoqpP04Hw8dxURALqNWQXj7+nZsN/8 sIju7U5VryA0vE5LCjQEC2IT7oWKSN1q8+iKtpk= X-Google-Smtp-Source: AHgI3IZdEfvBbATF1A6Uh59IaEiCg+Y6FLhGnJ+bAD/Be82Dcy+Qhat2e9cwp8IHi4sfV4sBNqrCqeecm+zcfQEO3xs= X-Received: by 2002:a05:660c:81a:: with SMTP id j26mr2133957itk.70.1549460216235; Wed, 06 Feb 2019 05:36:56 -0800 (PST) MIME-Version: 1.0 References: <595b374c-bc4f-9b8a-0013-6485abbfb477@php.net> In-Reply-To: Date: Wed, 6 Feb 2019 14:36:37 +0100 Message-ID: To: Benjamin Morel Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000c84922058139d108" Subject: Re: [PHP-DEV] Re: [RFC] [VOTE] Typed properties v2 From: nikita.ppv@gmail.com (Nikita Popov) --000000000000c84922058139d108 Content-Type: text/plain; charset="UTF-8" On Wed, Feb 6, 2019 at 1:38 PM Benjamin Morel wrote: > Hi Nikita, > > While playing with typed properties, something that annoys me a lot, in > the context of a data mapper, is not to be able to use isset() to check > whether an object's property is initialized. The issue was already there > with properties that have been explicitly unset(), but will be > exacerbated by typed properties, which will be unset *by default*. > > Sure, we have ReflectionProperty::isInitialized(), but reflection is slow > compared to direct property access, when reading a lot of objects. > > For example, check these 3 ways of reading initialized public/protected > object properties by name, along with their timings for 100,000 iterations > (benchmark here > > ): > > Using reflection: (890 ms, down to 384 ms using pre-instantiated > ReflectionProperty objects) > > $r = new \ReflectionClass($class); >> foreach ($props as $prop) { >> $p = $r->getProperty($prop); >> $p->setAccessible(true); >> if ($p->isInitialized($object)) { >> $values[$p->getName()] = $p->getValue($object); >> } >> } > > > Using array cast: (193 ms) > > foreach ((array) $object as $key => $value) { >> // Remove the "\0*\0" in front of protected properties >> $pos = strrpos($key, "\0"); >> if ($pos !== false) { >> $key = substr($key, $pos + 1); >> } >> $values[$key] = $value; >> } > > > Using a bound closure and isset(): (145 ms) > > >> (function() use ($props, & $values) { >> foreach ($props as $prop) { >> if (isset($this->{$prop})) { // skips NULL values as well :-( >> $values[$prop] = $this->{$prop}; >> } >> } >> })->bindTo($object, $object)(); > > > Unfortunately, while the last approach is the fastest, and IMO the > cleanest one, it is currently unusable because there is no way to > differentiate between an uninitialized property and a NULL property. > > Would it be possible to introduce another isset() operator, that would > return true for NULL values? > Note that this would also be useful when checking if an array key exists, > instead of having to switch from isset() to array_key_exists() when the > array may contain NULL values. > It's possible and someone even worked on an implementation at some point ( https://github.com/php/php-src/pull/1530), but I don't believe this ever proceeded to the RFC stage. (I'm rather doubtful about introducing something like this, due to the quite significant increase in conceptual language complexity for little benefit. I definitely have no plans to pursue such a feature myself.) Nikita > On Fri, 11 Jan 2019 at 17:38, Nikita Popov wrote: > >> On Mon, Oct 22, 2018 at 1:58 PM Sebastian Bergmann >> wrote: >> >> > Am 26.09.2018 um 15:46 schrieb Nikita Popov: >> > > I'm pleased to announce that the typed properties RFC has been >> accepted >> > > with 70 votes in favor and one vote against. We will work to finalize >> and >> > > merge the implementation in the next few days. >> > >> > Any update on when this will be merged? Thanks! >> > >> >> Sorry for the long delay here. I've spent the last week fixing the >> remaining issues and cleaning up the patch, and... >> >> ...Typed properties are now merged into master! [1] >> >> It would be great if people could start experimenting with typed >> properties >> (e.g. by migrating over existing @var annotations and seeing what >> happens). >> The earlier we find issues in the implementation or semantics, the better. >> >> Thanks to everyone who worked on this RFC and participated in discussions. >> Special thanks to Bob Weinand who did most of the initial implementation >> work on this RFC. >> >> Regards, >> Nikita >> >> [1]: >> >> https://github.com/php/php-src/commit/e219ec144ef6682b71e135fd18654ee1bb4676b4 >> > --000000000000c84922058139d108--