PHPackages                             arc/prototype - 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. arc/prototype

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

arc/prototype
=============

ARC: prototypical inheritance component

3.0.1(6y ago)015.7k13MITPHPPHP &gt;=7.1

Since Jan 10Pushed 5y ago2 watchersCompare

[ Source](https://github.com/Ariadne-CMS/arc-prototype)[ Packagist](https://packagist.org/packages/arc/prototype)[ Docs](https://github.com/Ariadne-CMS/arc/wiki)[ RSS](/packages/arc-prototype/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (6)Used By (3)

ARC: Ariadne Component Library
==============================

[](#arc-ariadne-component-library)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7ae9b4f36917317c38e84a3831b6b5e5510ff77348d5d85cd4ea91f7b228f8e5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f41726961646e652d434d532f6172632d70726f746f747970652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ariadne-CMS/arc-prototype/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/1119f356ebf445fe52f4eca6cb1c28d2859a00a4513333e3b59f3c6d67160df3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f41726961646e652d434d532f6172632d70726f746f747970652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ariadne-CMS/arc-prototype/)[![Latest Stable Version](https://camo.githubusercontent.com/41f52acfeea0d636c72ff327cabe6d94756781a9488ce9be8ea4152973c3631d/68747470733a2f2f706f7365722e707567782e6f72672f6172632f70726f746f747970652f762f737461626c652e737667)](https://packagist.org/packages/arc/prototype)[![Total Downloads](https://camo.githubusercontent.com/f0e9b17c1d36db3308857bf2b283a26ef41b020a6a56813c2af6d2a728ce4db8/68747470733a2f2f706f7365722e707567782e6f72672f6172632f70726f746f747970652f646f776e6c6f6164732e737667)](https://packagist.org/packages/arc/prototype)[![Latest Unstable Version](https://camo.githubusercontent.com/b6f41e3843529354e72eed0b35eac4d473f24b61dcaff6089d8d124a2d86c56e/68747470733a2f2f706f7365722e707567782e6f72672f6172632f70726f746f747970652f762f756e737461626c652e737667)](https://packagist.org/packages/arc/prototype)[![License](https://camo.githubusercontent.com/41b6f30835b1b8a6ee018925c22b4d5234e827fc6cd39f3017550557528771b3/68747470733a2f2f706f7365722e707567782e6f72672f6172632f70726f746f747970652f6c6963656e73652e737667)](https://packagist.org/packages/arc/prototype)

arc/prototype
=============

[](#arcprototype)

This component adds prototypes to PHP, with all the javascript features like Object.extend, Object.assign, Object.freeze and Object.observe. It also has support for setters and getters, defined per property.

Create a prototype object
-------------------------

[](#create-a-prototype-object)

```
    $object = \arc\prototype::create();
```

Adding properties
-----------------

[](#adding-properties)

```
    $object->foo = 'Foo';
```

Adding methods
--------------

[](#adding-methods)

```
    $object->bar = function() {
        return $this->foo.'bar';
    }
```

Extending objects
-----------------

[](#extending-objects)

```
    $childObject = \arc\prototype::extend($object);
    $childObject->foo = 'Vue';
    echo $childObject->bar();
```

Quick create
------------

[](#quick-create)

```
    $object = \arc\prototype::create([
        'foo' => 'Foo',
        'bar' => function() {
            return $this->foo.'bar';
        }
    ]);
```

Quick extend
------------

[](#quick-extend)

```
    $childObject = \arc\prototype::extend($object, [
        'foo' => 'Vue'
    ]);
```

Setters and Getters
-------------------

[](#setters-and-getters)

```
    $object->guarded = [
        'set' => function($value) {
            if ( $value !== 'Foo' ) {
                $this->unguarded = $value;
            }
        },
        'get' => function() {
            return 'Foo'.$this->unguarded;
        }
    ];
```

If you only have a 'set' function, getting the value will always return 'null'. If you only have a 'get' function, setting the value will do nothing.

Finalizing objects
------------------

[](#finalizing-objects)

```
    \arc\prototype::preventExtension($object);
    $childObject = \arc\prototype::extend($object);
```

This will throw a \\BadMethodCallException.

```
    $isExtensible = \arc\prototype::isExtensible($object); // returns false

```

Sealing objects
---------------

[](#sealing-objects)

```
    $object->foo = 'Foo';
    \arc\prototype::seal($object);
    $object->foo = 'Bar';
```

This will throw a \\LogicException

```
$isExtensible = \arc\prototype::isExtensible($object); // returns false
$isSealed = \arc\prototype::isSealed($object); // returns true

```

```

## Freezing objects

```php
    $object->foo = 'Foo';
    \arc\prototype::freeze($object);
    $object->foo = 'Bar';

```

This will throw a \\LogicException

```
    $isExtensible = \arc\prototype::isExtensible($object); // returns false
    $isSealed = \arc\prototype::isSealed($object); // returns true
    $isFrozen = \arc\prototype::isFrozen($object); // returns true

```

Observing changes
-----------------

[](#observing-changes)

```
    $log = [];
    \arc\prototype::observe($object, function($changes) use (&$log) {
        $log[] = $changes;
    });
```

Or limit the observer to specific types of changes:

```
    $log = [];
    \arc\prototype::observe($object, function($changes) use (&$log) {
        $log[] = $changes;
    }, ['add','delete']);
```

If not set, the full list of change types will be observed: 'add','update','delete','reconfigure'.

Setters, Getters and Superprivates
----------------------------------

[](#setters-getters-and-superprivates)

```
    function makeAFoo() {
        $superPrivate = 'Shhh...';
        $object = \arc\prototype::create();
        $object->foo = [
            'get' => function() use (&$superPrivate) {
                return $superPrivate;
            }
        ];
        $object->doFoo = function($value) use (&$superPrivate) {
            $superPrivate = 'Shhh... '.$value;
        };
        return $object;
    }
```

By using a variable that is not a property of the prototype object, but is in the scope of a number of the objects methods, you can create something like a private property. But it is even more private than a normal private property, even other methods in this object cannot access this variable. This is called a 'SuperPrivate' in javascript.

Using arc\\prototype as a Dependency Injection Container
--------------------------------------------------------

[](#using-arcprototype-as-a-dependency-injection-container)

```
