PHPackages                             einstein/open\_struct - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. einstein/open\_struct

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

einstein/open\_struct
=====================

Data structure that allows the definition of arbitrary methods and properties at runtime

0.0.2(12y ago)21381MITPHPPHP &gt;= 5.3.0

Since Jun 27Pushed 12y agoCompare

[ Source](https://github.com/einstein/open_struct)[ Packagist](https://packagist.org/packages/einstein/open_struct)[ Docs](https://github.com/einstein/open_struct)[ RSS](/packages/einstein-open-struct/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

OpenStruct
==========

[](#openstruct)

An `OpenStruct` is a data structure that allows the definition of arbitrary methods and properties **at runtime**.

Installation
------------

[](#installation)

If you're using `composer` simply add the `einstein/open-struct` dependency to your `composer.json` file.

Otherwise you can manually install it by cloning the repository somewhere in your php `include_path`.

```
git clone git@github.com:einstein/open_struct.git`
require 'open_struct/open_struct.php';

```

Usage
-----

[](#usage)

```
$person = new OpenStruct;
$person->name = 'John';
$person->age = 35;

echo $person->name;     # => 'John'
echo $person->age;      # => 35
echo $person->address;  # => null

```

`OpenStruct` uses an `array` internally to store properties and can even be initialized with one:

```
$person = new OpenStruct(array('name' => 'John', 'age' => 35));

echo $person->name;     # => 'John'
echo $person->age;      # => 35
echo $person->address;  # => null

print_r($person->properties);  # => array('name' => 'John', 'age' => 35)

```

`OpenStruct` objects can `extend` classes allowing new methods to be defined at runtime (`$this` even resolves correctly):

```
class AssetHelpers {
    function asset_path($path) {
        return '/assets/'.$path;
    }
}

$helpers = new OpenStruct;
$helpers->extend('AssetHelpers');

echo $helpers->asset_path('cat.png');  # => '/assets/cat.png'
echo $helpers->asset_path('dog.png');  # => '/assets/dog.png'

```

Methods can even be overridden (notice the call to `$this->super()` which references the example above):

```
class S3AssetHelpers {
    function asset_path($path) {
        return 'http://'.$this->s3_bucket.'.s3.amazonaws.com'.$this->super($path);
    }
}

$helpers->extend('S3AssetHelpers');
$helpers->s3_bucket = 'example';

echo $helpers->asset_path('cat.png');  # => 'http://example.s3.amazonaws.com/assets/cat.png'
echo $helpers->asset_path('dog.png');  # => 'http://example.s3.amazonaws.com/assets/dog.png'

print_r($helpers->ancestors);  # => array('S3AssetHelpers', 'AssetHelpers')

```

Note that `super` can only be called from within a method:

```
$struct = new OpenStruct;
$struct->super();  # => BadMethodCallException - Undefined method OpenStruct::super()

```

A wildcard/catch-all method `method_missing` can be defined as well (equivalent of PHP's `__call`)

```
$person = new OpenStruct(array('name' => 'Bob'));

$person->get_name();  # => BadMethodCallException - Undefined method OpenStruct::get_name()

class Getters {
    function method_missing($method, $arguments) {
        if (preg_match('/^get_(.+)$/', $method, $matches) && isset($this->{$matches[1]})) {
            return $this->{$matches[1]};
        } else {
            return $this->super($method, $arguments);
        }
    }
}

$person->extend('Getters');

$person->get_name();   # => 'Bob'
$person->get_age();    # => BadMethodCallException - Undefined method OpenStruct::get_age()
$person->undefined();  # => BadMethodCallException - Undefined method OpenStruct::undefined()

```

If you `extend` a class that has a static method called `extended`, it will be called and passed the current `OpenStruct` instance and any additional parameters passed to `extend` as arguments.

You can use it like a constructor/initializer:

```
class S3AssetHelpers {
    static function extended($struct, $bucket = 'example') {
        $struct->s3_bucket = $bucket;
    }

    function asset_path($path) {
        return 'http://'.$this->s3_bucket.'.s3.amazonaws.com/'.$path;
    }
}

$helpers = new OpenStruct;
$helpers->extend('S3AssetHelpers', 'test');

echo $helpers->asset_path('cat.png');  # => 'http://test.s3.amazonaws.com/cat.png'
echo $helpers->asset_path('dog.png');  # => 'http://test.s3.amazonaws.com/dog.png'

```

The `extend` method also accepts an `array` of classes to extend as the first argument. These method calls all do the same thing:

```
$struct = new OpenStruct;

$struct->extend(array('AssetHelpers', 'S3AssetHelpers'));

$struct->extend('AssetHelpers');
$struct->extend('S3AssetHelpers');

$struct->extend('AssetHelpers')->extend('S3AssetHelpers');

```

PHP has many [reserved keywords](http://php.net/manual/en/reserved.keywords.php) that throw fatal errors when you try to define them as methods e.g. `function include() { }`. `OpenStruct` allows you to define methods with keyword names by prefixing them with `__` e.g. `function __include() { }`. This allows you to call `$struct->include()` without throwing fatal errors.

`OpenStruct` also includes an `eval` method which accepts a `$file` and optional array of `$locals`. It will [extract](http://php.net/extract) any locals and [include](http://www.php.net/manual/en/function.include.php)`$file` within the context of the `OpenStruct` instance. It also accepts an optional third argument `$extract_properties`which will also extract references to all of the `OpenStruct` instance's properties before including `$file`. `$extract_properties` defaults to `false`.

Testing
-------

[](#testing)

`OpenStruct` tests require [jaz303/ztest](http://github.com/jaz303/ztest)

Simply download it to `open_struct/test/ztest` (or anywhere else in your PHP `include_path`), then run `test/run`

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

2

Last Release

4702d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9489f8e5ddf55b53780ff49cdd0d1a04b4f8d2530a1495d0edda265fa2b44c16?d=identicon)[shuber](/maintainers/shuber)

---

Top Contributors

[![shuber](https://avatars.githubusercontent.com/u/2419?v=4)](https://github.com/shuber "shuber (20 commits)")

---

Tags

modulestraitsmixinsstructoverloading

### Embed Badge

![Health badge](/badges/einstein-open-struct/health.svg)

```
[![Health](https://phpackages.com/badges/einstein-open-struct/health.svg)](https://phpackages.com/packages/einstein-open-struct)
```

###  Alternatives

[ddoe/wysiwyg-editor-module

Summernote WYSIWYG Editor for OXID eShop.

19981.7k2](/packages/ddoe-wysiwyg-editor-module)[oxid-esales/gdpr-optin-module

This is the GDPR opt-in module for the OXID eShop.

20459.1k2](/packages/oxid-esales-gdpr-optin-module)[zonneplan/laravel-module-loader

Module loader for Laravel

24118.4k](/packages/zonneplan-laravel-module-loader)[macopedia/typo3-interactive-tour

An interactive tour through the TYPO3 backend.

131.8k](/packages/macopedia-typo3-interactive-tour)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
