2017 © Pedro Peláez
 

library php-struct

Structs for PHP

image

ajbdev/php-struct

Structs for PHP

  • Monday, June 29, 2015
  • by ajbdev
  • Repository
  • 3 Watchers
  • 41 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Structs for PHP

Structs for PHP7 inspired by golang, (*1)

Usage

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

$user = new User();

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

$user['email'] = 'andybaird@gmail.com';

// 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:, (*2)

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., (*3)

Filling a struct from an array:, (*4)

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

You can extend structs further by giving them their own methods., (*5)

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:, (*6)

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., (*7)

The Versions

29/06 2015

dev-master

9999999-dev

Structs for PHP

  Sources   Download

BSD-2-Clause

The Requires

  • php >=7

 

by Andy Baird

struct