1--TEST-- 2ArrayObject with property hooks 3--FILE-- 4<?php 5 6class TestHooks 7{ 8 private bool $isModified = false; 9 public string $first { 10 get { 11 return strtoupper($this->first); 12 } 13 } 14 15 public function __construct(string $first, public string $last) { 16 $this->first = $first; 17 } 18 19 public string $fullName { 20 // Override the "read" action with arbitrary logic. 21 get => $this->first . " " . $this->last; 22 23 // Override the "write" action with arbitrary logic. 24 set { 25 [$this->first, $this->last] = explode(' ', $value, 2); 26 $this->isModified = true; 27 } 28 } 29 30 public string $username { 31 set(string $value) { 32 if (strlen($value) > 10) throw new \Exception('Too long'); 33 $this->username = strtolower($value); 34 } 35 } 36} 37 38$o = new TestHooks('first', 'last'); 39$a = new ArrayObject($o); 40 41echo 'Check object properties directly', PHP_EOL; 42var_dump($o->first); 43var_dump($o->last); 44var_dump($o->fullName); 45 46echo 'Check object properties via ArrayObject index', PHP_EOL; 47var_dump($a['first']); 48var_dump($a['last']); 49var_dump($a['fullName']); 50var_dump($a['username']); 51 52echo 'Write to object properties via ArrayObject index', PHP_EOL; 53$a['first'] = 'ArrayObject'; 54$a['last'] = 'ArrayObject'; 55$a['fullName'] = 'ArrayObject is hell'; 56$a['username'] = 'whatever_hooks_do_not_matter'; 57 58echo 'Check object properties directly', PHP_EOL; 59var_dump($o->first); 60var_dump($o->last); 61var_dump($o->fullName); 62var_dump($o->username); 63 64?> 65--EXPECTF-- 66Check object properties directly 67string(5) "FIRST" 68string(4) "last" 69string(10) "FIRST last" 70Check object properties via ArrayObject index 71string(5) "first" 72string(4) "last" 73 74Warning: Undefined array key "fullName" in %s on line %d 75NULL 76 77Warning: Undefined array key "username" in %s on line %d 78NULL 79Write to object properties via ArrayObject index 80Check object properties directly 81string(11) "ARRAYOBJECT" 82string(11) "ArrayObject" 83string(23) "ARRAYOBJECT ArrayObject" 84string(28) "whatever_hooks_do_not_matter" 85