armdevstack/strict-properties-access

Strict property access control in PHP.

Maintainers

Package info

github.com/Narek333888/strict-properties-access

pkg:composer/armdevstack/strict-properties-access

Statistics

Installs: 12

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.2.0 2025-07-22 23:57 UTC

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.