PHPackages                             sabloger/php-strict - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. sabloger/php-strict

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

sabloger/php-strict
===================

If you want PHP much clearer you'll be happy with Php-Strict! If you need a way to pass packed arguments to functions you'll be happy with Php-Strict! If you need a way to validate arguments and get rid of exceptions and data-misses on invalid arguments you'll be happy with Php-Strict! If you want a strict-typed ArrayList like Java and other strict-typed languages you'll be happy with Php-Strict!

0.1(8y ago)343[2 issues](https://github.com/sabloger/php-strict/issues)MITPHPPHP &gt;=5.6.0

Since Aug 1Pushed 8y ago1 watchersCompare

[ Source](https://github.com/sabloger/php-strict)[ Packagist](https://packagist.org/packages/sabloger/php-strict)[ RSS](/packages/sabloger-php-strict/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)DependenciesVersions (4)Used By (0)

php-strict
==========

[](#php-strict)

Strict-type Object base and Array-List for PHP 5! (5.4 and upper!)

[![License](https://camo.githubusercontent.com/6bf19a1b44ab9efd8b047856d9caf6d13fed27a07873736299ab6fbff611f667/68747470733a2f2f706f7365722e707567782e6f72672f7361626c6f6765722f7068702d7374726963742f6c6963656e73652e737667)](https://github.com/sabloger/php-strict/blob/master/LICENSE)[![Packagist](https://camo.githubusercontent.com/dc5ec4a54b978dc5adea4c040fdcca7a4bd84ee6d08a70b4ed6d31c425292bc9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7061636b61676973742d6465762d2d6d61737465722d6f72616e67652e737667)](https://packagist.org/packages/sabloger/php-strict)

Introduction
------------

[](#introduction)

- If you want PHP much clearer you'll be happy with Php-Strict!
- If you need a way to pass packed arguments to functions you'll be happy with Php-Strict!
- If you need a way to validate arguments and get rid of exceptions and data-misses on invalid arguments you'll be happy with Php-Strict!
- If you want a strict-type ArrayList like Java and other strict-typed languages you'll be happy with Php-Strict!
- Binding to HTTP request is comming soon!!

Installation
------------

[](#installation)

Add following code to your `composer.json` and run `composer update`:

```
{
  "require": {
    "sabloger/php-strict": "dev-master"
  }
}
```

Usage: Object
-------------

[](#usage-object)

First extend one sub class from `BaseObject`, implement parent abstract methods, set predefines, instantiate then use! Finally run `$obj->validate()` for validate data!. For example: Sub-class:

```
use Php_Strict\BaseObject;

/**
 * Class Book
 * @package Php_Strict
 * @method Book setTitle(string $value)
 * @method Book setAuthor(string $value)
 * @method Book setYear(int $value)
 * @method Book setSome_Object(Object $value)
 * @method string getTitle()
 * @method string getAuthor()
 * @method int getYear()
 * @method Object getSome_Object()
 */
class Book extends BaseObject
{
    /**
     * @return array
     */
    public function getFieldsStub()
    {
        return [
            'title' => 'string', // All of types as you want!!
            'author' => 'string',
            'year' => 'int',
            'some_object' => Object::class
        ];
    }

    /**
     * @return array
     */
    public function getRequiredFields()
    {
        return [
            'title',
            'year'
        ];
    }

    /**
     * If you want to allow setting undefined properties, set it True!
     * @return bool
     */
    protected function isUndefinedSettingAllowed()
    {
        return false;
    }
}
```

- For ease of use and get suggestions on IDE for fields setters and getters, I strongly suggesting write class level docs, as like as above sample.

Use:

```
$book = new Book(["Title" => "Advanced PHP Programming"]); // Its case-insensitive and auto case-correcter!! :)
$book->setAuthor("George Schlossnagle");
$book["Year"] = 2004;

$obj = new Object();
$obj->Foo = "bar";
$obj["foO"] = "BAR"; // Yessss its case-insensitive and auto case-correcter!!  ["Foo": "BAR"]

$book->set("some_object",$obj); // Several access methods!! Use these as you want!!

print $book; // It's echoable!!
//$book->toJson(); // Jsonable :)
//$book->toArray(); // Arrayable :)

foreach ($book as $key => $value) { // Iterateable :)
  echo "key: $key , value: $value \n";
}
/*
Out:
{"title":"Advanced PHP Programming","author":"George Schlossnagle","year":2004,"some_object":{"Foo":"BAR"}}
key: title , value: Advanced PHP Programming
key: author , value: George Schlossnagle
key: year , value: 2004
key: some_object , value: {"Foo":"BAR"}
*/
```

Usage: ArrayList
----------------

[](#usage-arraylist)

Can instantiate it directly and extend it for customization. For use only instantiate, setType and use!!

Basic use:

```
//Scalar:

$strArr = new ArrayList('string');
$strArr[] = "correct!";
$strArr[] = 123; //PHP Fatal error:  Uncaught exception 'Php_Strict\Exceptions\InvalidItemTypeException' with message 'Invalid item type exception: Expected type was "string" given "integer"!'

print $strArr->toJson();

//Object:
$obj = new Object();
$obj->Foo = "bar";
$obj["foO"] = "BAR";

$arr = new ArrayList(Object::class);

//$arr[0] = 11; //PHP Fatal error:  Uncaught exception 'Php_Strict\Exceptions\InvalidItemTypeException' with message 'Invalid item type exception: Expected type was "Php_Strict\Object" given "integer"!'

$arr[0] = $obj;
$obj->foo = "bar";
$arr[1] = $obj;
$arr[0]->baz = "bag";
$arr[2] = $obj;
print $arr . "\n";
print $obj; // Its by-reference!
```

Advanced use:

```
use Php_Strict\ArrayList;

class BookArrayList extends ArrayList
{
    /**
     * @param array $items
     * @return BookArrayList
     */
    public static function newLib(array $items = [])
    {
        return (new self($items));
    }

    /**
     * BookArrayList constructor.
     * @param array $items
     */
    public function __construct(array $items = [])
    {
        parent::__construct(Book::class, $items);
    }

    /**
     * Validate all of items using Object->validate() and ArrayList each()
     * @throws \Exception
     * @return null|true
     */
    public function validate()
    {
        parent::each(function (Book $item, $key) { // ArrayList has each(closure) method! :)
            try {
                $item->validate();
            } catch (\Exception $exception) {
                throw new \Exception(sprintf('ArrayList validation failed at offset (%s) with message: %s' , $key, $exception->getMessage()));
            }
        });
        return true;
    }
}
```

Use:

```
$book = new Book(["Title" => "Advanced PHP Programming"]);
$book->setAuthor("George Schlossnagle");

$bookArr = BookArrayList::newLib();
$bookArr[] = $book;
$bookArr->validate(); // PHP Fatal error:  Uncaught exception 'Exception' with message 'ArrayList validation failed at offset (0) with message: Required fields are not filled. unfilled required fields: (["year"])'

foreach ($bookArr as $book) { // Iterateable :)
  echo "item: $book  \n";
}
```

LICENSE
-------

[](#license)

This library is released under the [MIT license](https://github.com/sabloger/php-strict/blob/master/LICENSE).

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance7

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

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

Unknown

Total

1

Last Release

3132d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a1e01829e45cb4f2a3477d7596769e2af0abf20b793c7fef48f8ad1a63fdbad3?d=identicon)[sabloger](/maintainers/sabloger)

---

Top Contributors

[![sabloger](https://avatars.githubusercontent.com/u/17683967?v=4)](https://github.com/sabloger "sabloger (32 commits)")

---

Tags

arrayarraylistcase-insensitivecomposer-packagehelperiterationjsonobject-oriented-programmingphpphp5strict-typesvalidation

### Embed Badge

![Health badge](/badges/sabloger-php-strict/health.svg)

```
[![Health](https://phpackages.com/badges/sabloger-php-strict/health.svg)](https://phpackages.com/packages/sabloger-php-strict)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[nette/forms

📝 Nette Forms: generating, validating and processing secure forms in PHP. Handy API, fully customizable, server &amp; client side validation and mature design.

54013.2M450](/packages/nette-forms)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)

PHPackages © 2026

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