The phpABLE struct emulation library.
- PHP >= 8.0.0
- able/helpers
- able/prototypes
- able/exceptions
- able/reglib
The mission of this library is to emulate the structures' behavior most naturally.
There's a simple way to install the able/struct
package via composer:
composer require able/struct
Let's try to declare a structure:
use \Able\Struct;
class MyStruct extends AStruct {
protected static $Prototype = ['field1', 'field2'];
}
Now we can use it in a siple way:
$Struct = new MyStruct(1,2);
echo $Struct->field1;
//> 1
It's also possible to fill fields later:
$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.
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:
$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.
$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.
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.
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:
$Struct = new MyChildStruct(1,2,3);
echo $Struct->field1;
echo $Struct->field2;
echo $Struct->field3;
//> 1
//> 2
//> 3
To retrieve all structure keys:
$Struct->keys();
To retrieve all structure values:
$Struct->values();
To copy all data into an array:
$Struct->toArray();
To get fields count:
$Struct->count();
To clean all fields and restore its default values:
$Struct->flush();
If you use a PHPDoc-friendly IDE you can gain additional advantages by using the syntax below:
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.