Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:130646 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 2A8211A00BC for ; Wed, 15 Apr 2026 18:18:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1776277116; bh=9iVd2aF0Ary34lvhpD7e4793+8i2tZNIH3YFnReBq+c=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=HWlIzvg4lTmQY7hhNo1iuVIf5sLN9sgo8k7yzaGdBKzkaj8/iSaKL+0TCUDFfbktH BPbyVC/AOHZBbzLQqep8lK1BGTvoqEIkMY+SkSruUDw3OjGQHXf7gRqnQcF0xHRgg+ AZ1kXsV71NaUS5zX2dq2wx+mTiGBgzMy+00+ofyrv/2xX3/yzeaaLbBM8YvEnCQR3l /p29f5UJlDDw3PaHKvjewDyat+uTLhBcyptsC5ysNS7Bss2RM+NxIVXlFXbM4NmGdy Z57S7EpuaKNMWCi6fpMJ4OjcKxw9obT12suOhT7RZwteVMLJs71QeOGLunfj9dHXMG BJyY/x2qxcBdA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3CEE61801DB for ; Wed, 15 Apr 2026 18:18:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 15 Apr 2026 18:18:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1776277103; bh=7CQ+iGDgsZdIaLOmOuVj5HZTmZliI49EHfnV6qHeqMs=; h=MIME-Version:Date:From:To:Cc:Subject:In-Reply-To:References: Message-ID:Content-Type:from:to:cc:subject:message-id; b=KrQlWxQt+exblF19Me2CCCnqjoBbJJc4ei81FojUSxur3QQgtLcTIrUg9CQVw/1F6 zxxpBQID4ArhQwjRUql4AsDSu4FsbdhXYJGUvz+T4Hw3OieBgYfXE4acaxupHYCSBs BX1V1Hb39uWvFzcX7DC/yHb4/tQ2i278pZLntb0UohV9pOvLO99tpCI2tnlx/IJgrT gzECnVStcd+Budj3TGPW46CIIJ3Tsm80/p0G+hT6dho/wd/13N84MM3M/RJpBYuIwf PKkOXrTHNdOA7mQ+TwHnwsPSYKeTB75sv/d91GubzIilq/g5o2ndOu4nA8FiepAp61 1DjE7vLa84wGw== Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Wed, 15 Apr 2026 20:18:22 +0200 To: Barel Cc: php internals Subject: Re: [PHP-DEV] [RFC] [Discussion] array_get and array_has functions In-Reply-To: References: Message-ID: <19d58a46b50222b75da4e40e417efd05@bastelstu.be> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi Am 2026-04-14 19:06, schrieb Barel: > - I see the point that Tim made about the function names, I have > updated > them to array_get_path() and array_has_path() Similarly to Rob's tongue in cheek comment, I think I would also prefer having a common prefix for the functions, since this improves discoverability. While there are some inconsistencies in the array API due to its age, I think there is a reasonably similar precedent with the `array_key_*()` functions. In fact looking at those, I wonder if the following pair of names would make sense for your proposed feature: - array_path() - array_path_exists() From what I see, the functions in the array API don't mention “get” as the default operation, e.g. it's `array_first()`, not `array_get_first()`. And for “existence checks” there's the obvious precedent with `array_key_exists()` which is also used in the example implementation in your RFC. > - Regarding the issue of returning the default if an intermediate > segment > is not an array, the philosophy of the function is: if the path exists, > return the value, otherwise return the default. This cover uses cases > like > this: in yaml configuration many times you set a config option to ~ > (null) > and this many times indicates "use the default config". So you may have > something like: > > database: > config: ~ > > Indicating that you want to use the default database config. If you > convert > this yaml file to an array an use the array_get_path function to get a > value, for example array_get_path($config, ['database', 'config', > 'port'], > 7766), you want to get 7766, not an exception or error. Tim points out > that > intermediate segments might be objects which implement ArrayAccess, but > allowing this would complicate the code of the function a lot without > much > practical gain. Perhaps the right solution is using “isset”-like semantics instead: `null` is treated as an absent entry (it can be debated if an explicit `null` at the end of the path should be treated as `null` rather than the default), array allows the traversal to continue and everything else results in a clear error. This would then allow to extend support to anything ArrayAccess in the future without a compatibility break. Generally “throwing an error” is always a safe option. But saying that `array_get_path(['foo' => '???'], ['foo', 'bar'], 'default');` should result in `'default'` doesn't make sense to me and - staying with the config example - might mask configuration errors: As an example, a user might accidentally configure a "DSN": database: connection: "mysql://root@db.example.com" when in reality the library expects separate components: database: connection: driver: mysql host: db.example.com user: root Now extracting the host with `array_get_path($config, ['database', 'connection', 'host'], 'localhost')` would erroneously fall back to default values instead of reporting a clear error. Best regards Tim Düsterhus