Created
July 11, 2026 07:58
-
-
Save NickSdot/bf093e875be9ebeccf34ad4f82191349 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| declare(strict_types=1); | |
| final class PipelineOne // private(set) | |
| { | |
| public private(set) array $orderedPipelineSteps = [ | |
| \Thing\StepDoSomethingTwo::class, | |
| \Thing\StepAtReportBeginning::class, | |
| \Thing\StepDoSomethingOne::class, | |
| ]; | |
| public function describe(): array | |
| { | |
| \sort($this->orderedPipelineSteps); // whoopsie, should have copied first | |
| $ordered = $this->orderedPipelineSteps; | |
| // do stuff | |
| return $ordered; | |
| } | |
| } | |
| final readonly class PipelineTwo // readonly | |
| { | |
| public function __construct( | |
| public array $orderedPipelineSteps = [ | |
| \Thing\StepDoSomethingTwo::class, | |
| \Thing\StepAtReportBeginning::class, | |
| \Thing\StepDoSomethingOne::class, | |
| ], | |
| ) {} | |
| public function describe(): array | |
| { | |
| \sort($this->orderedPipelineSteps); // whoopsie, should have copied first | |
| $ordered = $this->orderedPipelineSteps; | |
| // do stuff | |
| return $ordered; | |
| } | |
| } | |
| // ###################### private(set) ###################### | |
| $one = new PipelineOne(); | |
| \var_dump($one->describe()); | |
| // array(3) { | |
| // [0]=> | |
| // string(27) "Thing\StepAtReportBeginning" // good, now ordered | |
| // [1]=> | |
| // string(24) "Thing\StepDoSomethingOne" | |
| // [2]=> | |
| // string(24) "Thing\StepDoSomethingTwo" | |
| //} | |
| \var_dump($one->orderedPipelineSteps); | |
| // array(3) { | |
| // [0]=> | |
| // string(27) "Thing\StepAtReportBeginning" // whoopsie, this should NOT be first! | |
| // [1]=> | |
| // string(24) "Thing\StepDoSomethingOne" | |
| // [2]=> | |
| // string(24) "Thing\StepDoSomethingTwo" | |
| //} | |
| // ###################### readonly ###################### | |
| $two = new PipelineTwo(); | |
| \var_dump($two->describe()); | |
| //Fatal error: Uncaught Error: Cannot indirectly modify readonly property PipelineTwo::$orderedPipelineSteps | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment