public
Last active

PHP Struct class

  • Download Gist
Struct.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?php
class Struct
{
/**
* Define a new struct object, a blueprint object with only empty properties.
*/
public static function factory()
{
$struct = new self;
foreach (func_get_args() as $value) {
$struct->$value = null;
}
return $struct;
}
/**
* Create a new variable of the struct type $this.
*/
public function create()
{
// Clone the empty blueprint-struct ($this) into the new data $struct.
$struct = clone $this;
// Populate the new struct.
$properties = array_keys((array) $struct);
foreach (func_get_args() as $key => $value) {
if (!is_null($value)) {
$struct->$properties[$key] = $value;
}
}
// Return the populated struct.
return $struct;
}
}
example1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12
<?php
require 'Struct.php';
 
// define a 'coordinates' struct with 3 properties
$coords = Struct::factory('degree', 'minute', 'second' ,'pole');
// create 2 latitude/longitude numbers
$lat = $coords->create(35, 41, 5.4816, 'N');
$lng = $coords->create(139, 45, 56.6958, 'E');
// use the different values by name
echo $lat->degree . '° ' . $lat->minute . "' " . $lat->second . '" ' . $lat->pole;
example2.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
require 'Struct.php';
 
// define a struct
$struct1 = Struct::factory('var1', 'var2');
// create 2 variables of the 'struct1' type
$instance0 = $struct1->create('val0-1', 'val0-2');
$instance1 = $struct1->create('val1-1', 'val1-2');
$instance2 = $struct1->create('val2-1'); // var2 will be null
// use the variables later on in a readable manner
echo $instance1->var2;
example3.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
require 'Struct.php';
 
// define a struct with a default value
$struct2 = Struct::factory('var3', 'var4');
$struct2->var3 = 'default';
// create 2 variables of the 'struct2' type
$instance3 = $struct2->create('val3-1', 'val3-2');
$instance4 = $struct2->create('val4-1', 'val4-2');
$instance5 = $struct2->create(null, 'val5-2'); // null becomes the default value
// use the variables later on in a readable manner
echo $instance4->var3;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.