Giter VIP home page Giter VIP logo

php-strict-array's Introduction

Prepare for future with php-strict-array

According to this article which shows how to create strictly typed arrays and collections in Php7, php-strict-array was born.


There is some discusion about generic in Php but who knows when it comes to us.

It is not exacly what you know from Java or C++ where generic looks like array<bool>() or array<string, \Foo\User>().

Here strict arrays looks like array_int_, array_bool_ and array_string_and_float_ so I hope when they come to nativ Php all what you need to do will be:

  1. Replace all new array_User, new array_string_and_array_object__ to array<User>, array<string, array<object>>.

Now you do not have to validate array from user inside your packages:

function foo(array $users)
{
    foreach ($users as $user) {
        if (!$user instanceof User) {
            thorw new \InvalidArgumentException();
        }
    }

    ...
}

Your code can be much simpler:

function foo(array_User_ $users)
{
    ...
}

Install

composer require eXtalion/php-strict-array

Base strict arrays

Some base arrays are generated:

  • array_array_,
  • array_bool_,
  • array_callable_,
  • array_float_,
  • array_int_,
  • array_iterable_,
  • array_object_,
  • array_string_,
  • array_string_and_array_,
  • array_string_and_bool_,
  • array_string_and_callable_,
  • array_string_and_float_,
  • array_string_and_int_,
  • array_string_and_iterable_,
  • array_string_and_object_,
  • array_string_and_string_.

Create new strict array

Run:

vendor/bin/php-sa [path/to/arrays-dir]

and answer to fee questions. Without path/to/arrays-dir code will be display to STDOUT.

It's "normal" array

There are some array functions which works with strict arrays:

  • sarray_filter,
  • sarray_key_exists,
  • sarray_keys,
  • sarray_search,
  • sarray_values.

php-strict-array's People

Contributors

d0niek avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.