armdevstack / strict-properties-access
Strict property access control in PHP.
Package info
github.com/Narek333888/strict-properties-access
pkg:composer/armdevstack/strict-properties-access
Requires
- php: >=8.2
- ext-ctype: *
- ext-mbstring: *
This package is not auto-updated.
Last update: 2026-04-15 03:06:15 UTC
README
Enforce strict control over property access in your PHP classes. Prevent accidental creation of dynamic properties and provide detailed handling for invalid property interactions using customizable logging and observation tools.
๐ฆ Package Overview
StrictPropertiesAccess provides:
- A trait (
StrictPropertyAccess) for strict property access enforcement. - An abstract class (
AbstractStrictModel) to simplify inheritance. - Interfaces for customization:
LoggerInterface,PropertyAccessObserverInterface. - Built-in implementations:
ErrorLogger,DebugObserver.
๐ Installation
Install via Composer:
composer require armdevstack/strict-properties-access
๐ Quick Start
โ Recommended: Extend the Abstract Class
use ArmDevStack\StrictPropertiesAccess\Classes\Base\AbstractStrictModel; class User extends AbstractStrictModel { public string $name; public string $email; } $user = new User(); $user->name = 'John'; // โ OK echo $user->email; // โ OK echo $user->age; // โ Triggers observer/log/exception
โ๏ธ Alternative: Use the Trait Directly
use ArmDevStack\StrictPropertiesAccess\Traits\StrictPropertyAccess; class Product { use StrictPropertyAccess; public string $title; } $product = new Product(); $product->title = 'Laptop'; // โ OK echo $product->price; // โ Invalid access
โ๏ธ Features
๐ Strict Mode
- Enabled by default.
- Disables dynamic properties.
- Triggers warning/observer/exception on access to undefined properties.
๐ข Error Handling Modes
- Echo: Output messages directly.
- Log: Send to error log (via logger).
- Both (default): Echo + log.
๐ง Observers & Loggers
Attach custom loggers or observers to control error behavior or monitor access attempts.
๐ ๏ธ Configuration API
Enable/Disable Strict Mode
$object->enableStrictMode(); // Enable strict enforcement $object->disableStrictMode(); // Disable enforcement
Enable/Disable Exceptions
$object->enableExceptions(); // Throws LogicException on invalid access $object->disableExceptions(); // Echo/log instead
Set Logger
use ArmDevStack\StrictPropertiesAccess\Loggers\ErrorLogger; $logger = new ErrorLogger(); $object->setLogger($logger);
Set Observer
use ArmDevStack\StrictPropertiesAccess\Observers\DebugObserver; $observer = new DebugObserver(); $object->setPropertyAccessObserver($observer);
Set Error Output Mode
$object->setErrorOutputMode('echo'); // echo | log | both
๐ค Debugging Tools
Track Invalid Accesses
$invalid = $object->getInvalidAccesses(); print_r($invalid);
๐ Custom Handler: handleMissingProperty()
If your class defines a method named handleMissingProperty(string $property), it will be automatically invoked when a non-existent property is accessed.
This allows you to override the default error behavior with custom logic:
class MyModel extends AbstractStrictModel { public string $title; protected function handleMissingProperty(string $property): void { echo "Custom handler: '$property' was accessed but does not exist!" . PHP_EOL; } } $model = new MyModel(); echo $model->nonExistent; // Triggers handleMissingProperty()
- This method is optional.
- Only triggered if strict mode is enabled and property does not exist.
- Takes precedence over observers and default echo/log/error behavior.
๐งฉ Interfaces
StrictPropertyAccessInterface
public function enableStrictMode(): void; public function disableStrictMode(): void; public function enableExceptions(): void; public function disableExceptions(): void; public function getInvalidAccesses(): array;
LoggerInterface
public function log(string $message): void;
PropertyAccessObserverInterface
public function onMissingProperty(string $property); public function onDynamicPropertyCreationAttempt(string $property, $value);
๐๏ธ Extend with Your Own Classes
Custom Observer Example
use ArmDevStack\StrictPropertiesAccess\Contracts\Observers\PropertyAccessObserverInterface; class CustomObserver implements PropertyAccessObserverInterface { public function onMissingProperty(string $property) { // Your custom logic } public function onDynamicPropertyCreationAttempt(string $property, $value) { // Custom alerting/logging } }
๐ Example Use Case: Preventing Bugs in DTOs
class PaymentDTO extends AbstractStrictModel { public string $amount; public string $currency; } $dto = new PaymentDTO(); $dto->amount = '100'; // Oops! Typo $dto->currncy = 'USD'; // โ Will trigger strict mode warning/exception
๐งช Unit Testing Tip
For test environments, you can disable strict mode:
$dto->disableStrictMode();
๐งพ License
MIT ยฉ ArmDevStack
๐ Contributing
Pull requests are welcome. For major changes, please open an issue first.
๐ซ Support
For bugs or suggestions, open an issue on GitHub or contact vardapetyannarek0@gmail.com.