PHP Struct class

This class gives you the ability to easily create (emulate) structs in php.

<?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;
    }
}

How to use

Place the class in your framework's library or something and include it. Do not put it in the same source file as the code that makes use of it. Eww.

Example 1 - Geographic coordinates

<?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;

Example 2 - Simple struct with a 'null' value

<?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;

Example 3 - Simple struct with a default value

<?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;

Feedback

Ohh boy. There has been some muppet giving me negative feedback on my implementation, however he did so without argumentation. I feel I need to clarify the need for a struct.

Constructive feedback is welcome, on twitter for example. I'm not interested in people telling me I don't get OOP. Make sure you know what you are talking about. Also, feedback comes in various forms, interface and implementation are 2 different things, make sure you state clearly what you mean.

And I'll admit, declaring the methods final was a bit much, I removed it.

When to use

  • When you need a kind of 'template' class to build structs in php.
  • When you have little data, and it feels like it belongs together logically.
  • When an array gives to you too much duplicate code. When using a array in example 1, you have to specify the key names multiple times, or use (magical) numbered keys.
  • When you don't want to implement a entire class. Because the described data is too simple/small for example.
  • When you want data format definition code and data processing code together, because it does not make sense when pulled apart. In PHP you cannot nest classes, hence the factory method.
  • When you don't want to repeat yourself.

When not to use

  • When the data belongs in a database. Use Database Models.
  • When the data comes with functionality, create your own object.
  • When the data is used on multiple places/source files in your code.
  • When you're not using OOP, just use an array.