smuuf / strict-object
只是一个简单的 strict-object 特性。
1.0.1
2021-02-21 16:21 UTC
Requires
- php: ^7.4|^8
Requires (Dev)
- nette/tester: ^2.3
- phpstan/phpstan: ^0.12.67
This package is auto-updated.
Last update: 2024-09-22 00:25:32 UTC
README
避免 PHP 在处理未声明的对象属性时过于宽松而引起的烦恼问题。让我们更加严格。不要让属性名错误再次毁了你的一天。🌞
安装
composer require smuuf/strict-object
用法
简单,只需在类中使用 use
\Smuuf\StrictObject
特性使其严格 - 并且当有人尝试 读取 或 写入 一个 未声明属性 时,将抛出 \LogicException
。
<?php use \Smuuf\StrictObject; require __DIR__ . '/../bootstrap.php'; class Whatever { use StrictObject; public $someProperty; } $obj = new Whatever; $obj->someProperty = 1; // Ok. echo $obj->someProperty; // Ok. // But - and hold on to your hats... $obj->bogusProperty = 1; // Throws \LogicException. echo $obj->bogusProperty; // Throws \LogicException.