Skip to content
PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
LICENSE.md
composer.json
phpunit.xml
readme.md
scrutinizer.yml
travis.yml

readme.md

📢 Yell

Scrutinizer Code Quality Code Climate Latest Stable Version License

PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects.

Requirement

PHP >= 5.4.0.

Installation

You can install the library using the following ways:

Using Composer

You can install this through Composer, a dependency manager for PHP. Just run the below command:

composer require zeeshanu/yell

For further details you can find the package at Packagist.

Manual way

  • Copy src to your codebase, perhaps to the vendor directory.
  • Add the Zeeshanu\Yell\Scream class to your autoloader or require the file directly.

Getting Started

I'm going to create take an example to demonstrate the usage.

For example, you have a Person class like below:

class Person
{
    public $name;
    public $age;
}

$person = new Person();

$person->name = 'John Doe';
$person->age = 23;

// Will silently set the property `profession` on `Person` without any issue 
$person->profession = 'Teacher';

Here is how you can make your object strict i.e. not allow any other properties except $name and $age and throw exceptions for any other properties. Just use Zeeshanu\Yell\Scream trait in your class as follows

use Zeeshanu\Yell\Scream;

class Person
{
    use Scream;

    public $name;
    public $age;
}

$person = new Person();

$person->name = 'John Doe';
$person->age = 23;

// An exception will be thrown when showing message "Trying to set undefined property $profession in class Person"  
$person->profession = 'Teacher';

Feedback

If you notice that there might be some improvements in code you can create a pull request or report an issue. You can also contact me at ziishaned@gmail.com.

License

The MIT License (MIT). Please see License File for more information.

You can’t perform that action at this time.