Giter Club home page Giter Club logo

php-struct's Introduction

Structs for PHP

Structs for PHP7 inspired by golang

Usage

struct('User', [
    'name'      =>  'string',
    'age'       =>  'int',
    'active'    =>  'bool',
]);

$user = new User();

$user['name'] = 'Andy';
$user['age'] = 13;
$user['active'] = true;

$user['email'] = '[email protected]';

// Fatal error: Uncaught InvalidArgumentException: Struct does not contain property `email`

$user['age'] = '22';

// Fatal error: Uncaught TypeException: Argument 1 passed to User::set_age() must be of the type integer, string given

Turn off strict type checking and allow variables to be coerced into types by simply calling:

Struct\Struct::$strict = false;
$user['age'] = '22';
var_dump($user['age']);

// int(22)

Under the hood, structs are simply classes implementing ArrayAccess and Iterable generated at run time. They have generated getter and setters for all fields that allow them to do the type checking.

Filling a struct from an array:

$row = $db->fetchArray('select * from user where id=1');
$user->fromArray($row);

You can extend structs further by giving them their own methods.

struct('User', [
    'firstName'         =>  'string',
    'lastName'          =>  'string',
    'active'            =>  'bool',
    'age'               =>  'int'
],[
    'fullName'          =>  function() {
        return $this['firstName'] . ' ' . $this['lastName'];
    }
]);

$user['firstName'] = 'Andy';
$user['lastName'] = 'Baird';

echo $user->fullName();
// Andy Baird

Add magic methods simply:

struct('User', [
    'firstName'         =>  'string',
    'lastName'          =>  'string',
    'active'            =>  'bool',
    'age'               =>  'int'
],[
    '__toString'          =>  function() {
        return $this['firstName'] . ' ' . $this['lastName'] . ' is a ' . $this['age'] . ' year old ' . ($this['active'] ? 'active' : 'inactive') . ' user';
    }
]);

echo $user;
// Andy Baird is a 13 year old inactive user

But... why?

Just for my own experimentation. I would love to see structs implemented as a core feature of PHP, as I can see them being very appropriate for a more procedural or functional style of programming.

php-struct's People

Contributors

ajbdev avatar

Stargazers

Diego Magdaleno avatar Peter Vreča avatar gmling avatar Sébastien Monterisi avatar Andrew Winter avatar Bad Sir Brian avatar Matt Royce avatar tebe avatar Ben Rowe avatar Rolando Guedes avatar Fulvio Carvalhido avatar Daniel Rosengren avatar Michael Chrisco avatar George Cooksey avatar Hari K T avatar Akihito Koriyama avatar sasezaki avatar Саша avatar Dmytro Zarezenko avatar  avatar [riftlab] ErgoZ avatar Ilya I. Averkov avatar Dmitry Danilson avatar Denys K avatar Daniel Plakinger avatar Cody avatar Freek Van der Herten avatar  avatar Nick avatar Sebastian De Deyne avatar İzni Burak Demirtaş avatar Hüseyin Mert avatar Antonin Januska avatar Aaron Godin avatar Jeroen Visser avatar Travis van der F. avatar Rick Wong avatar Ian Jenkins avatar Sébastien Vanvelthem avatar Simon Dann avatar Steven Wade avatar Paul Saunders avatar Mark van Eijk avatar Dave Marshall avatar Payton Bice avatar Trevor N. Suarez avatar

Watchers

 avatar Fulvio Carvalhido avatar James Cloos avatar Travis van der F. avatar

Forkers

travisfont

php-struct's Issues

struct variables

Ideally, shouldn't structs be implemented as objects rather than arrays?

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.