PHPackages                             jgswift/magery - 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. jgswift/magery

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

jgswift/magery
==============

PHP 5.4+ magic interception system using traits

0.1.1(11y ago)115MITPHPPHP &gt;=5.4

Since Sep 11Pushed 11y ago2 watchersCompare

[ Source](https://github.com/jgswift/magery)[ Packagist](https://packagist.org/packages/jgswift/magery)[ RSS](/packages/jgswift-magery/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

magery
======

[](#magery)

PHP 5.4+ magic interception system using traits

[![Build Status](https://camo.githubusercontent.com/8ce190d2e96a7cb1887f80dfad15699b35a3b558d152aab4d066c99162955824/68747470733a2f2f7472617669732d63692e6f72672f6a6773776966742f6d61676572792e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/jgswift/magery)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/d52d3f88739093705e0b05ef4c154d6a40b6b7b164035fff79af36dbbb9ccfda/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6773776966742f6d61676572792f6261646765732f7175616c6974792d73636f72652e706e673f733d30396563663464353938646664623764393930373065376261386137643139376162646466616531)](https://scrutinizer-ci.com/g/jgswift/magery/)[![Latest Stable Version](https://camo.githubusercontent.com/da11d15b86221ce6e24af245a2e9fe63b62de115215885917f9237c811f8fa46/68747470733a2f2f706f7365722e707567782e6f72672f6a6773776966742f6d61676572792f762f737461626c652e737667)](https://packagist.org/packages/jgswift/magery)[![License](https://camo.githubusercontent.com/ff45a0afa86575637aa8c5017429094f519dfe31f3eaa1395350f982415d791e/68747470733a2f2f706f7365722e707567782e6f72672f6a6773776966742f6d61676572792f6c6963656e73652e737667)](https://packagist.org/packages/jgswift/magery)[![Coverage Status](https://camo.githubusercontent.com/a208cfb635b21e3e8b97e8c895ce11b5feeedbe00194fb85e72d06a7e95872c5/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6a6773776966742f6d61676572792f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/jgswift/magery?branch=master)

Description
-----------

[](#description)

magery provides a trait framework with which to hook into object magic, namely \_\_get, \_\_set, \_\_unset, \_\_isset, and \_\_call.

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

[](#installation)

Install via cli using [composer](https://getcomposer.org/):

```
php composer.phar require jgswift/magery:0.1.*
```

Install via composer.json using [composer](https://getcomposer.org/):

```
{
    "require": {
        "jgswift/magery": "0.1.*"
    }
}
```

Dependency
----------

[](#dependency)

- php 5.4+

Usage
-----

[](#usage)

### Basic

[](#basic)

```
class Foo
{
    use Magery\Mage;

    private $bar;

    public function __construct()
    {
        $this->read('bar', function(){
            throw new \Exception('Don\'t touch my bar!');
        });
    }

    public function touchBar()
    {
        $this->bar;
    }
}

$foo = new Foo();
$foo->touchBar(); // Fatal error: Uncaught exception 'Exception' with message 'Don't touch my bar!'
```

### Write

[](#write)

A write callback could be registered to protect a variable from being overwritten (even from within the scope of your class)

```
$this->write('bar', function() {
  throw new \Exception('Don\'t write to my bar!');
});

public function writeToBar() {
  $this->bar = 'somethingElse';
}

$foo = new Foo();
$foo->writeToBar(); // Fatal error: Uncaught exception 'Exception' with message 'Don't write to my bar!'
```

### Read

[](#read)

It is possible to intercept any object property or method with an event.
Note: Multiple registered events will fire in the order they were added (FIFO) until an event returns a response value.

```
class Foo {
    use magery\Mage;

    public function __construct() {
        $this->read('bar', function(){
            return 'baz';
        });

        // Shortcut method
        $this->read('buzz', function() {
            return 'bar';
        });
    }
}

$foo = new Foo();
echo $foo->bar;     // 'baz'
echo $foo->buzz;    // 'bar'
```

### Exists

[](#exists)

```
class User {
    private $firstName;
    private $lastName;

    function __construct($firstName, $lastName) { /* ... */ }
}

$user = new User('John', 'Doe');
$user->exists('lastName', function()use(&$c) {
    return isset($this->lastName);
    // do something extra for existence check
});

var_dump(isset($user->lastName)); // true
```

### Remove

[](#remove)

```
$user = new User('Joe','Smith');
$user->remove('name', function()use(&$c) {
    unset($this->name);
    // do something extra for remove
});

unset($user->name);

var_dump(isset($user->name)); // false
```

### Result Caching

[](#result-caching)

If the event returns a response, this may be cached to reduce execution time on future reads.

```
public $bar = 'Bill';

public function __construct()
{
  $this->read('bar', function(){
    sleep(1);
    return microtime();
  }, true);     // pass in "true" here (defaults to false)
}

$foo = new Foo();
var_dump($foo->bar === $foo->bar);   // true
```

### Helper Method Scope

[](#helper-method-scope)

All events are registered globally except the magery function is protected unless otherwise specified.

```
