2017 © Pedro Peláez
 

library struct

phpABLE struct data type implementation library

image

able/struct

phpABLE struct data type implementation library

  • Tuesday, June 19, 2018
  • by able
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Introduction

The phpABLE struct emulation library., (*1)

Requirements

Features

The mission of this library is to emulate the structures' behavior most naturally., (*2)

Install

There's a simple way to install the able/struct package via composer:, (*3)

composer require able/struct

Usage

Basic

Let's try to declare a structure:, (*4)

use \Able\Struct;

class MyStruct extends AStruct {

    protected static $Prototype = ['field1', 'field2'];
}

Now we can use it in a siple way:, (*5)

$Struct = new MyStruct(1,2);
echo $Struct->field1;

//> 1

It's also possible to fill fields later:, (*6)

$Struct = new MyStruct();

$Struct->field1 = "Test string!";
echo $Struct->field1;

//> Test string!

Mutators

Mutators are pretty helpful in case it needed to customize the default structure behavior., (*7)

use \Able\Struct;

class MyStruct extends AStruct {

    protected static $Prototype = ['field1', 'field2'];

    protected final function setField1Property($value) {
        return 'The mutated via setter value is: ' . $value;
    }

    protected final function getField2Property($value) {
        return 'The mutated via getter value is: ' . $value;
    }
}

Let's test it:, (*8)

$Struct = new MyStruct(1,2);

echo $Struct->field1;
echo $Struct->field2;

//> The mutated via setter value is: 1
//> The mutated via getter value is: 2

The next example just illustrates the difference between setters and getters., (*9)

$Data = $Struct->toArray();

echo $Data['field1'];
echo $Data['field2'];

//> The mutated via setter value is: 1
//> 2

Default values

The default values could be set via constants., (*10)

use \Able\Struct;

class MyParentStruct extends AStruct {

    protected static array $Prototype = ['field1', 'field2'];

    protected const defaultField1Value = "default value for field1";
    protected const defaultField2Value = "default value for field2";
}

Inheritance

The inheritance level isn't limited. All fields defined at parent classes will also be accessible at child classes., (*11)

use \Able\Struct;

class MyParentStruct extends AStruct {

    protected static array $Prototype = ['field1', 'field2'];
}

class MyChildStruct extends MyParentStruct {

    protected static array $Prototype = ['field3'];
}

It perfectly works:, (*12)

$Struct = new MyChildStruct(1,2,3);

echo $Struct->field1;
echo $Struct->field2;
echo $Struct->field3;

//> 1
//> 2
//> 3

Advanced

To retrieve all structure keys:, (*13)

$Struct->keys();

To retrieve all structure values:, (*14)

$Struct->values();

To copy all data into an array:, (*15)

$Struct->toArray();

To get fields count:, (*16)

$Struct->count();

To clean all fields and restore its default values:, (*17)

$Struct->flush();

IDEs support

If you use a PHPDoc-friendly IDE you can gain additional advantages by using the syntax below:, (*18)

use \Able\Struct;

/**
 * @property int field1
 * @property string field2
 */
class MyStruct extends AStruct {

    protected static array $Prototype = ['field1', 'field2'];
}

License

This package is released under the MIT license., (*19)

The Versions

19/06 2018

dev-master

9999999-dev

phpABLE struct data type implementation library

  Sources   Download

MIT

The Requires

 

The Development Requires

by hacpaka

struct structures phpable