Skip to content

Instantly share code, notes, and snippets.

@rlerdorf
Last active October 22, 2025 22:40
Show Gist options
  • Select an option

  • Save rlerdorf/307f690732a4795d9eca7c2ada372fd9 to your computer and use it in GitHub Desktop.

Select an option

Save rlerdorf/307f690732a4795d9eca7c2ada372fd9 to your computer and use it in GitHub Desktop.
Phan Demo - PHP 84, Phan v6-dev
{
"phpVersion": "84",
"phanVersion": "v6-dev",
"astVersion": "1.1.3",
"plugins": "310592423266",
"fileOrder": [
"PropertyHooks.php"
]
}
<?php
/**
* Basic property hook tests
* @phan-file-suppress PhanUnreferencedClass, PhanUnreferencedPublicProperty, PhanUnreferencedPrivateProperty, PhanWriteOnlyPublicProperty
*/
class PropertyHookBasic {
// Simple get hook
public string $upperName {
get => strtoupper($this->upperName);
}
// Simple set hook with parameter
public string $lowerName {
set(int $value) {
$this->lowerName = strtolower($value);
}
}
// Both get and set hooks
public int $value {
get => $this->storage;
set(int $val) {
$this->storage = $val * 2;
}
}
private int $storage = 0;
// Virtual property (hooks without backing storage)
public string $computed {
get { return $this->firstName . ' ' . $this->lastName; }
}
private string $firstName = 'John';
private string $lastName = 'Doe';
// Hook with full block body
public string $formatted {
get {
$result = ucfirst($this->formatted);
return $result;
}
}
// Final hook
public string $finalProp {
final get => $this->finalProp;
}
}
// Test property hook in interface (abstract hooks without body)
interface PropertyHookInterface {
public string $name {
get;
set;
}
}
// Test in abstract class
abstract class AbstractPropertyHookClass {
// Abstract hook in abstract class
abstract public string $abstractProp {
get;
}
}
// Test readonly property restriction
class ReadonlyTest {
// This is fine - readonly without hooks
public readonly string $validReadonly;
public function __construct(string $value) {
$this->validReadonly = $value;
}
}
new ReadonlyTest("banana")->validReadonly = "apple";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment