Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126100 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 B5BAE1A00BD for ; Wed, 4 Dec 2024 17:16:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1733332390; bh=xgNFGk0QBbWtLIlD46T6lniw7z/+oboTYegATJ8kIFc=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=QqTeC8wP2ox0JKZJy+cjbBQ5KPUrsAQTjGDcdA3onOaRtXKlcv7EOLm6kDxIqV0nv oCTbs3w5ku9Untz5Cx2fQT5QB1Bc8gbwvKtxbhjwEcaBrB4vzGGkjphlZVTJTJdnS3 sNnSmE1dcuDDHMhceE8NBoCdosFnQ2my/geG4DIykMMjEUxEiiPOQjttZ/fhhkbGiG yf/bv+1KKCuQXvtI+NGqU0gOk2MVrM3KAWmYlQXTuKJcv4j3L8tTQIorNbey36rmdM vldonyzait8eQOEP22jHKwk85EMcpPn3aRU4UOIBrCc94bv+31qC/hB90ib+h7MT7C 0fQcYMdQOik0g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E869A180074 for ; Wed, 4 Dec 2024 17:13:09 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (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, 4 Dec 2024 17:13:06 +0000 (UTC) Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-2ee6abf124aso41185a91.3 for ; Wed, 04 Dec 2024 09:16:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733332573; x=1733937373; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Ej/sefghlvhpUZrTw+oVhHt+H45YYtTP4zhJ8/WCBOw=; b=DJAmdj1xbSjf7ALFQiPjG+Oc+3cnFVlTEZQaQwBENBVYZW3Tl9HZTCzq8G6LgBr4ht tXPuLJZUNYFL6eKX/6cGa4bj7gKbrcgfuZS+ikK9TTcWlzufiiKX9NTKm6jfpA1oY9nP +mj5cq7vMfFNXSg/6xSyDa1QZQrMFC8pt0moQct7FWN+TeO8TwEkUCqfAMgvkRyekn9z OTgNOSQl7HPkAbmTpsCBtEv2xXJogTrnA7y1+WF0onH2UbYtJQ1MtblXPSC3DA4RPV7y TJHo/r9y7f61+9vJo0KSGWwV6k3//C/mJOgJhgS5Vj+T7+tdlFYVKBE2uR2gijqiABIx MN6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733332573; x=1733937373; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Ej/sefghlvhpUZrTw+oVhHt+H45YYtTP4zhJ8/WCBOw=; b=W8plwSDf0MjS04ddkixI3RrTQMfvGOdUUHRz5MUxYS/wVKrUgMaJkeoPxounx5F2j0 jvst63ui53Tfy1RlQNv/7IyGmuyGl6kG5M/T7JU3HC+z8T9tOk+2bbpAN+VK410H0muP GCr1NTNc18cKpZ9810uF3F3fgKZEj7CmMvNIJGTKd5sha8ITAqrtnWJL/S6PqOhJ4jGk n5LjOUeJ8IWU9WKBNY1V/Kq2IFaWow7pDrXZW+zOGI3pMWzDfPWW7ib4K4kz/W4qBbod sVrQx5T19l6R0OkiNkTCp3NGLFsNJIweD6txiRE5u8FzB/DwBrSMn/Ieu+2iq2e8fKg+ Ej4Q== X-Gm-Message-State: AOJu0Yx5th1WfpIIXLEU/4PsOEyf1wGNT1YtyLWfWJ48tt3bqjf1w0Y8 EFQVtGuAxyMR9vBWNvf+afdXVlBNf76RndrrJ92Blt84Cb6m5r3E51+kec/YgzsxaYIiVov2dPo GoEj+Xs0/p9p9L5O5u6bBvah0Vmg= X-Gm-Gg: ASbGncsMFLPKDe3hc2dvBG2o4C9wRc757kArBMhJDIr02askMVbecgY6sMigRGiGt3z XpwJeNEktzWYeEsD2ZCAJ4IMH8J4D0+Bf X-Google-Smtp-Source: AGHT+IHSJuc0jrN4mUChQkp4tQzPWWcrSQjvRs0cnPNkUE1QSzhoH4Od6+LIEcD5bqlczkRRqqox7WF4OpzjSA9ftNw= X-Received: by 2002:a17:90b:3a92:b0:2ee:9d49:3aef with SMTP id 98e67ed59e1d1-2ef01241e85mr8336637a91.23.1733332572783; Wed, 04 Dec 2024 09:16:12 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 4 Dec 2024 14:15:36 -0300 Message-ID: Subject: Re: [PHP-DEV] Multiple property assigning without constructor To: Taras Chornyi Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000474b72062874f087" From: ericklima.comp@gmail.com (Erick de Azevedo Lima) --000000000000474b72062874f087 Content-Type: text/plain; charset="UTF-8" > > $person = new Person("Name", 43) { > firstName: "John", > age: 42 > }; Hi! You could achieve similar functionality with a simple user-land method "assign". If you put such a method on a trait, you can use it everywhere. See the example: $propVal) { $this->$propName = $propVal; } return $this; } } class Person { use AssignsPropertiesValues; public function __construct( public string $firstName, public int $age ) { } } class Person2 { use AssignsPropertiesValues; public string $firstName; public int $age; } class Person3 { use AssignsPropertiesValues; public string $firstName { set => \strtoupper($value); } public int $age; } $p1 = new Person('Pig',5)->assign(firstName: 'Peppa', age: 6); $p2 = new Person2()->assign(firstName: 'George', age: 3); $p3 = new Person3()->assign(firstName: 'Danny', age: 7); var_dump($p1, $p2, $p3); https://3v4l.org/F83NO#v8.4.1 Best regards, Erick --000000000000474b72062874f087 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
$person =3D new Person("Name", 43) {
=C2=A0 =C2=A0 =C2=A0 f= irstName: "John",
=C2=A0 =C2=A0 =C2=A0 age: 42
=C2=A0=C2=A0 };
<= br>

Hi!

You could achieve similar functionality with = a simple user-land method "assign". If you put such a method on a= trait, you can use it everywhere.

See the example:

<?php<= br>
trait AssignsPropertiesValues
{
=C2=A0 =C2=A0 public function = assign(...$props): static
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0= foreach ($props as $propName =3D> $propVal) {
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 $this->$propName =3D $propVal;
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0= return $this;
=C2=A0 =C2=A0 }
}

class Person
{
=C2=A0 = =C2=A0 use AssignsPropertiesValues;
=C2=A0 =C2=A0
=C2=A0 =C2=A0 publ= ic function __construct(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public string $firs= tName,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 public int $age
=C2=A0 =C2=A0 ) {<= br>=C2=A0 =C2=A0 }
}

class Person2
{
=C2=A0 =C2=A0 use Assi= gnsPropertiesValues;

=C2=A0 =C2=A0 public string $firstName;
=C2= =A0 =C2=A0 public int $age;
}

class Person3
{
=C2=A0 =C2=A0= use AssignsPropertiesValues;

=C2=A0 =C2=A0 public string $firstName= { set =3D> \strtoupper($value); }
=C2=A0 =C2=A0 public int $age;
= }

$p1 =3D new Person('Pig',5)->assign(firstName: 'Pep= pa', age: 6);
$p2 =3D new Person2()->assign(firstName: 'Georg= e', age: 3);
$p3 =3D new Person3()->assign(firstName: 'Danny&= #39;, age: 7);

var_dump($p1, $p2, $p3);

https://3v4l.org/F83NO#v8.4.1

--000000000000474b72062874f087--