dev-master
9999999-devStruct features emulation for php.
MIT
The Requires
- eggbe/helpers dev-master
- eggbe/prototype dev-master
- php >=7.1.0
The Development Requires
by hacpaka
struct structures php-struct structures emulation structure-like
Wallogit.com
2017 © Pedro Peláez
Struct features emulation for php.
The phpABLE struct emulation library., (*1)
The mission of this library is to emulate the structures' behavior most naturally., (*2)
There's a simple way to install the able/struct package via composer:, (*3)
composer require able/struct
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 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
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";
}
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
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();
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'];
}
This package is released under the MIT license., (*19)
Struct features emulation for php.
MIT
struct structures php-struct structures emulation structure-like