Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Strict Objects

Strict Objects Laravel Package

kdyby/strict-objects

kdyby/strict-objects provides a PHP trait that makes objects strict: reading or writing undeclared properties throws an exception instead of silently creating dynamic ones. It catches typos and enforces explicit property declarations in Nette projects.

View on GitHub
Deep Wiki
Context7

Simple trait to make your class strict, when calling or accessing undefined member (property or method).

Frequently asked questions about Strict Objects
What does kdyby/strict-objects actually do?
It provides a PHP trait that supplies the magic member-access hooks (__get, __set, __isset, __unset, __call and __callStatic) so that reading, writing or calling an undeclared member throws a MemberAccessException instead of PHP silently returning null or dispatching the call. The goal is to catch typos and force classes to declare their public surface explicitly. It is a single-responsibility behaviour trait with no framework coupling, container binding or configuration.
How do I install kdyby/strict-objects in a project?
Install it with composer require kdyby/strict-objects, then add the package trait via a use statement inside each class you want to make strict. There is no service provider, no container binding, no config file and no publish step. Installation is measured in developer-minutes per class; the real cost is auditing each adopting class for existing magic-method behaviour.
Can I use kdyby/strict-objects on Eloquent models or Macroable classes?
No. Eloquent models rely on __get, __set and __isset to back attributes, casts and relations, and Macroable consumers rely on __call for macro dispatch. Trait methods win silently over inherited ones, so adding strict-objects there displaces the framework's hooks and breaks the class in ways that surface far from the change. The same warning applies to Illuminate\Support\Fluent, Doctrine entities with proxy hydration and container property-injection targets.
Which classes in a Laravel app are good candidates for the strict-objects trait?
Good candidates are classes whose public surface is fully declared and constructor-injected: hand-written domain value objects such as Money, Email or DateRange, non-Eloquent DTOs, command and query handlers, and small result objects. Avoid anything that inherits magic member behaviour from a parent class or another trait, and avoid Blade-facing view models that lean on null-coalescing. Verify each class individually before adopting the trait.
What exception is thrown for an undefined property or method?
Reading, writing, checking or unsetting an undeclared property, and calling an undeclared method, raises a MemberAccessException rather than returning null or dispatching blindly. That turns a silent typo into a loud runtime failure. The trade-off is that you get a runtime error where static analysis could have given you a build-time one.
Does kdyby/strict-objects support PHP 8.2, 8.3, 8.4 and Laravel 10 or 11?
There is no evidence of PHP 8.2+ dynamic-property work or a Laravel 10/11 test matrix; the last release was 2020-12-17. The trait body itself is version-agnostic plain PHP, but the 2020 composer.json may cap PHP below your runtime or pin dev dependencies that conflict in a modern Laravel dependency graph. Test composer update on a branch and never use --ignore-platform-reqs as a workaround.
What license is kdyby/strict-objects released under?
Composer metadata reports NOASSERTION, meaning the license is not machine-identifiable. Kdyby's historical packages were permissive, but that is not an approval. A human needs to read the LICENSE file before this package can clear a standard OSS intake review. Treat the license question as blocking until it is resolved.
How does the trait affect PHPStan and Psalm static analysis?
Declaring __get and __set marks a class as magic to PHPStan and Psalm, which then stop reporting genuinely undefined properties on it. In a codebase already running PHPStan at level 6 or above, the trait can therefore be net-negative: it trades a build-time error for a runtime error on exactly the classes where you wanted static guarantees. Measure the overlap with what your current analysis level already catches before adopting it.
How do isset(), empty() and json_encode() behave with strict objects?
This is not documented in the available material, so read the trait source and test it per class: the key question is whether __isset throws or returns false for undefined members, because code in the wild probes with isset() and expects a boolean. Also note that json_encode and reflection bypass __get entirely, so a strict object can serialize successfully without the member that reading it would throw on. Hydrators and serializers using get_object_vars or reflection need explicit tests.
Is kdyby/strict-objects still maintained, and what are the alternatives?
Maintenance signals are weak: last release December 2020, around 31 stars, zero dependents and a single maintainer, with no security policy or visible modern-PHP CI. Alternatives include enabling PHPStan or Psalm strict rules, relying on natively declared properties and readonly, or forking the trait, which is only tens of lines and trivially reviewable. Forking is also the strongest mitigation available for the abandonment risk.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections