Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125269 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 qa.php.net (Postfix) with ESMTPS id 197CB1A00BD for ; Mon, 26 Aug 2024 12:28:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1724675450; bh=BogQlJWXvsI12FJDf7GV3GUK1BtkZLc8Pac3XO3APXE=; h=Date:From:To:In-Reply-To:References:Subject:From; b=R11N7B9hhpXyoQGAPaC0IfWfIG7Y0ZD6jctzBEQI+kzBplHQA2ajppqTxvRVzZ7mn N7IYdo/FYJCVqD42aO8V+z9iM+z+vYoFELBJzYWCkVlcMeV3zSleKiSOt4x0TA7RQt UBJOi9ZWvhQ2xsrp6fqV6og3bHxJr8KeMPP/mVzACN0s0hB6+t0CtVqBUN82r1rn8n 2qv7BnecP1MNYE14ELzblM/SPsoaok8EnotCgsk4tzA2YjHowBvUyPCalrZwH17W7v SIQX1XJGFRrunfRYdaxOM/ui69FJbIC2I9E/lbY6yJJxoml6hEmB+IiyS5L7NpK9iU 52QZh8ILmENMQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 78C31180059 for ; Mon, 26 Aug 2024 12:30:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh1-smtp.messagingengine.com (fhigh1-smtp.messagingengine.com [103.168.172.152]) (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 ; Mon, 26 Aug 2024 12:30:48 +0000 (UTC) Received: from phl-compute-03.internal (phl-compute-03.nyi.internal [10.202.2.43]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 823D31151ADF for ; Mon, 26 Aug 2024 08:28:55 -0400 (EDT) Received: from phl-imap-10 ([10.202.2.85]) by phl-compute-03.internal (MEProxy); Mon, 26 Aug 2024 08:28:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1724675335; x=1724761735; bh=FPiu/tZIAllCtGlqPHC/6lhqCWoK7iN105ZpKB57X3s=; b= iDeLZ3J0YeGggQOH9Ep3OvZuZAQXrlPXvQwmWQ2Idlp5umNPrj7+yFq0nGACOME9 iKkW23LiS9scU4w2QnaHYA+d4AwamAIGD3wHd+AdHZi+tsBUvE/aCJbd5LsnS3Cq TYFWKaXoobjo5IQuloCRZkmOW0J5hg2RWDZltORupxNfOiGKe0RSnfxRuZoQsEVr axECjMG5WvDO88LCry/4e/ma3+cLD8ATb9ZMvQyUkctI1M5ZwWFhZzSN+9Tdb81U FJ1g36KGbYI0uMosyIi1ZK4QbzGmHSS5hmgy2nqg4FWhnzDCF4jVab3HuC3oCQKl MlOHpzewA3IvtezySWF2og== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1724675335; x= 1724761735; bh=FPiu/tZIAllCtGlqPHC/6lhqCWoK7iN105ZpKB57X3s=; b=q 21dZZ2HloB+AGxUHE6g+0xE2OMvYYqxxlDiR2PFy7POrNMJ4rsLtwrnxndTtHfg3 soM/TRM61tlSWd06NNwjLgBNeQHYonPiKBBVLcj7s2ehxp5ecrdGR6tiUanrLd2S zbgZMFXVTtXT0FEWrXR4QL1aqlDZ8MLEAtkWsYzxbDGgjTaH6eyWIK9/RX/GNJFl 9qVNYTUI4Rv7+DP5mVb/vq+1Sb5Tp190nyk9qK+ZtHmMOCaIgRhuDSCYiX3Rp4LJ 7iIYp1ULz1Up5OOl5riBzUcZ0M34tvwnIvREn3CF+bEUb3K2aSck8GnthA3uu/Xy X4/6S2CsHLKe3TeaYT00A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddruddvkedgheduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefoggffhf fvkfgjfhfutgfgsehtjeertdertddtnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhn shculgfkoffuohfrngdfuceoihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenuc ggtffrrghtthgvrhhnpeevjefgvdehffeuudelgfeutdehteefhedtffdthfetvedulefg ueeiteelffeuvdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfh hrohhmpehimhhsohhprdhphhhpsehrfigvtgdrtghordhukhdpnhgspghrtghpthhtohep uddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhish htshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 3B8833C0066; Mon, 26 Aug 2024 08:28:55 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Mon, 26 Aug 2024 13:28:35 +0100 To: internals@lists.php.net Message-ID: <4c3bc918-3aec-4cc5-b342-0924e10949bc@app.fastmail.com> In-Reply-To: <07E4ABD5-B9CE-47FA-BD9E-E42A82CFC737@newclarity.net> References: <99846F78-38F8-4549-B0A6-6310933AA78D@newclarity.net> <0B974990-05FE-4C46-9C2B-3C243C7E8E81@getmailspring.com> <07E4ABD5-B9CE-47FA-BD9E-E42A82CFC737@newclarity.net> Subject: Re: [PHP-DEV] [RFC] Default expression Content-Type: text/plain Content-Transfer-Encoding: 7bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On Mon, 26 Aug 2024, at 11:43, Mike Schinkel wrote: >> You ask how a library can provide access to that default, and the answer is generally pretty trivial: define a public constant, and refer to it in the parameter definition. > > A global? Really? I didn't say "global", I said "public". Since you're keen on real-world examples, here's a simplified version of a real class: class ConfigSource { private HttpClientInterface $httpClient; public const DEFAULT_CONSUL_URL = 'http://localhost:8500'; public function __construct(private string $consulUrl=self::DEFAULT_CONSUL_URL, ?HttpClientInterface $httpClient=null) { if ( $httpClient === null ) { $httpClient = new HttpClient(); $httpClient->setRequestTimeoutSecs(5); $httpClient->setConnectRequestTimeoutSecs(5); } $this->httpClient = $httpClient; } } This constructor has two optional parameters; one of them uses a default which is also referenced explicitly in another class, so is exposed as a constant; the other uses a completely opaque method of creating a default object, which even Reflection could not expose. The caller doesn't need to know how any of this works to make use of the class. The contract is "__construct(optional string $consulUrl, optional ?HttpClientInterface $httpClient)" The purpose of the optional parameters is so that you can write "$configSource = new ConfigSource();" and trust the library to provide you a sensible default behaviour. If it was decided that the code for creating a default HttpClient was needed elsewhere, it could be refactored into a method, with appropriate access: public function __construct(private string $consulUrl=self::DEFAULT_CONSUL_URL, ?HttpClientInterface $httpClient=null) { $this->httpClient = $httpClient ?? $this->getDefaultHttpClient(); } public function getDefaultHttpClient(): HttpClient { $httpClient = new HttpClient(); $httpClient->setRequestTimeoutSecs(5); $httpClient->setConnectRequestTimeoutSecs(5); return $httpClient; } Or perhaps the HttpClient becomes nullable internally: public function __construct(private string $consulUrl=self::DEFAULT_CONSUL_URL, private ?HttpClientInterface $httpClient=null) {} Or maybe we allow explicit nulls, but default to a simple class instantiation: public function __construct(private string $consulUrl=self::DEFAULT_CONSUL_URL, private ?HttpClientInterface $httpClient=new HttpClient) {} None of these are, currently, breaking changes - the contract remains "__construct(optional string $consulUrl, optional ?HttpClientInterface $httpClient)". Regards, --- Rowan Tommins [IMSoP]