PHPackages                             armdevstack/strict-properties-access - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. armdevstack/strict-properties-access

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

armdevstack/strict-properties-access
====================================

Strict property access control in PHP.

1.2.0(10mo ago)012MITPHPPHP &gt;=8.2

Since Jul 18Pushed 10mo agoCompare

[ Source](https://github.com/Narek333888/strict-properties-access)[ Packagist](https://packagist.org/packages/armdevstack/strict-properties-access)[ RSS](/packages/armdevstack-strict-properties-access/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

StrictPropertiesAccess
======================

[](#strictpropertiesaccess)

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
------------------

[](#-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
--------------

[](#-installation)

Install via Composer:

```
composer require armdevstack/strict-properties-access
```

---

🚀 Quick Start
-------------

[](#-quick-start)

### ✅ Recommended: Extend the Abstract Class

[](#-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

[](#️-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
-----------

[](#️-features)

### 🔐 Strict Mode

[](#-strict-mode)

- Enabled by default.
- Disables dynamic properties.
- Triggers warning/observer/exception on access to undefined properties.

### 📢 Error Handling Modes

[](#-error-handling-modes)

- **Echo:** Output messages directly.
- **Log:** Send to error log (via logger).
- **Both (default):** Echo + log.

### 🔧 Observers &amp; Loggers

[](#-observers--loggers)

Attach custom loggers or observers to control error behavior or monitor access attempts.

---

🛠️ Configuration API
--------------------

[](#️-configuration-api)

### Enable/Disable Strict Mode

[](#enabledisable-strict-mode)

```
$object->enableStrictMode();    // Enable strict enforcement
$object->disableStrictMode();   // Disable enforcement
```

### Enable/Disable Exceptions

[](#enabledisable-exceptions)

```
$object->enableExceptions();    // Throws LogicException on invalid access
$object->disableExceptions();   // Echo/log instead
```

### Set Logger

[](#set-logger)

```
use ArmDevStack\StrictPropertiesAccess\Loggers\ErrorLogger;

$logger = new ErrorLogger();
$object->setLogger($logger);
```

### Set Observer

[](#set-observer)

```
use ArmDevStack\StrictPropertiesAccess\Observers\DebugObserver;

$observer = new DebugObserver();
$object->setPropertyAccessObserver($observer);
```

### Set Error Output Mode

[](#set-error-output-mode)

```
$object->setErrorOutputMode('echo'); // echo | log | both
```

---

📤 Debugging Tools
-----------------

[](#-debugging-tools)

### Track Invalid Accesses

[](#track-invalid-accesses)

```
$invalid = $object->getInvalidAccesses();
print_r($invalid);
```

### 🔍 Custom Handler: `handleMissingProperty()`

[](#-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
------------

[](#-interfaces)

### `StrictPropertyAccessInterface`

[](#strictpropertyaccessinterface)

```
public function enableStrictMode(): void;
public function disableStrictMode(): void;
public function enableExceptions(): void;
public function disableExceptions(): void;
public function getInvalidAccesses(): array;
```

### `LoggerInterface`

[](#loggerinterface)

```
public function log(string $message): void;
```

### `PropertyAccessObserverInterface`

[](#propertyaccessobserverinterface)

```
public function onMissingProperty(string $property);
public function onDynamicPropertyCreationAttempt(string $property, $value);
```

---

🏗️ Extend with Your Own Classes
-------------------------------

[](#️-extend-with-your-own-classes)

### Custom Observer Example

[](#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
-------------------------------------------

[](#-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
------------------

[](#-unit-testing-tip)

For test environments, you can disable strict mode:

```
$dto->disableStrictMode();
```

---

🧾 License
---------

[](#-license)

MIT © [ArmDevStack](https://github.com/armdevstack)

---

🙌 Contributing
--------------

[](#-contributing)

Pull requests are welcome. For major changes, please open an issue first.

---

📫 Support
---------

[](#-support)

For bugs or suggestions, open an issue on GitHub or contact [vardapetyannarek0@gmail.com](mailto:svardapetyannarek0@gmail.com).

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance55

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

3

Last Release

300d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/673715bfcfe9a58bdd31c49a0e25c6870d8a4dec3a32007eaf4e173f51f6d30b?d=identicon)[armdevstack1995](/maintainers/armdevstack1995)

---

Top Contributors

[![Narek333888](https://avatars.githubusercontent.com/u/117542052?v=4)](https://github.com/Narek333888 "Narek333888 (6 commits)")

### Embed Badge

![Health badge](/badges/armdevstack-strict-properties-access/health.svg)

```
[![Health](https://phpackages.com/badges/armdevstack-strict-properties-access/health.svg)](https://phpackages.com/packages/armdevstack-strict-properties-access)
```

###  Alternatives

[nojimage/twitter-text-php

A library of PHP classes that provide auto-linking and extraction of usernames, lists, hashtags and URLs from tweets.

1241.9M7](/packages/nojimage-twitter-text-php)[drupol/composer-packages

Composer Packages is a Composer plugin for getting information about installed packages in your project.

32274.0k1](/packages/drupol-composer-packages)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
