note 129362 added to language.types.object

From: Date: Fri, 29 Mar 2024 22:20:56 +0000
Subject: note 129362 added to language.types.object
Groups: php.notes 
Request: Send a blank email to php-notes+get-221262@lists.php.net to get a copy of this message
I subscribe that the correct form of declaring objects in modern PHP similarly to JS, is using the array-cast form, like this: <?php $object = (object) [ 'string' => 'foo', 'child' => (object) [ 'float' => 1.0, ], ]; echo $object->child->float . PHP_EOL; ?> This should be the preferred way instead of using associative arrays from now and on. Another way of implementing it by returning multiple values from a function call: <?php function math_operations(float $a, float $b): stdClass { return (object) [ 'addition' => $a + $a, 'subtraction' => $a - $a, ]; } $math = math_operations( a: 2.0, b: 2.0, ); echo $math->addition . PHP_EOL; echo $math->subtraction . PHP_EOL; ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.131 (proxied: 181.61.245.104) ---- Manual Page -- https://php.net/manual/en/language.types.object.php Edit -- https://main.php.net/note/edit/129362 Del: integrated -- https://main.php.net/note/delete/129362/integrated Del: useless -- https://main.php.net/note/delete/129362/useless Del: bad code -- https://main.php.net/note/delete/129362/bad+code Del: spam -- https://main.php.net/note/delete/129362/spam Del: non-english -- https://main.php.net/note/delete/129362/non-english Del: in docs -- https://main.php.net/note/delete/129362/in+docs Del: other reasons-- https://main.php.net/note/delete/129362 Reject -- https://main.php.net/note/reject/129362 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221262) next »