|
| 1 | +--TEST-- |
| 2 | +Hooks work on backed readonly properties |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +interface DbConnection { |
| 7 | + public function loadCategory(string $id): Category; |
| 8 | +} |
| 9 | + |
| 10 | +class Category { |
| 11 | + public function __construct(public string $name) {} |
| 12 | +} |
| 13 | + |
| 14 | +class MockDbConnection implements DbConnection { |
| 15 | + public function loadCategory(string $id): Category { |
| 16 | + echo "hit database\n"; |
| 17 | + return new Category("Category {$id}"); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +readonly class LazyProduct |
| 22 | +{ |
| 23 | + private DbConnection $dbApi; |
| 24 | + private string $categoryId; |
| 25 | + |
| 26 | + public Category $category { |
| 27 | + get { |
| 28 | + return $this->category ??= $this->dbApi->loadCategory($this->categoryId); |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +$product = new LazyProduct(); |
| 34 | +$reflect = new ReflectionClass($product); |
| 35 | + |
| 36 | +$db = $reflect->getProperty('dbApi'); |
| 37 | +$db->setAccessible(true); |
| 38 | +$db->setValue($product, new MockDbConnection()); |
| 39 | + |
| 40 | +$categoryId = $reflect->getProperty('categoryId'); |
| 41 | +$categoryId->setAccessible(true); |
| 42 | +$categoryId->setValue($product, '42'); |
| 43 | + |
| 44 | +// lazy loading, hit db |
| 45 | +$category1 = $product->category; |
| 46 | +echo $category1->name . "\n"; |
| 47 | + |
| 48 | +// cached value |
| 49 | +$category2 = $product->category; |
| 50 | +echo $category2->name . "\n"; |
| 51 | + |
| 52 | +// Verify same instance is returned (backing field caching works) |
| 53 | +var_dump($category1 === $category2); |
| 54 | + |
| 55 | +?> |
| 56 | +--EXPECT-- |
| 57 | +hit database |
| 58 | +Category 42 |
| 59 | +Category 42 |
| 60 | +bool(true) |
0 commit comments