Skip to content

Instantly share code, notes, and snippets.

@kaja47
Last active December 18, 2015 21:19
Show Gist options
  • Select an option

  • Save kaja47/5846724 to your computer and use it in GitHub Desktop.

Select an option

Save kaja47/5846724 to your computer and use it in GitHub Desktop.
Atrox\Access examples
<?php
$_ = new Atrox\Access();
$_->property;
// generates:
function ($x) { return $x->property; };
$_->method($args);
// generates:
function ($x) { return $x->method($args); };
$_['key'];
// generates:
function ($x) { return $x['key']; };
// you can nest thins as your heart desires
$_['key1']['key2']->prop->meth($x)->getSomething();
// which generates equivalent of this:
function ($x) { return $x['key1']['key2']->prop->meth($x)->getSomething(); };
<?php
class A {
public $i;
function __construct($i) {
$this->i = $i;
}
function mm($a) {
return $this->i + $a;
}
function getId() {
return spl_object_hash($this);
}
}
$arrays = [
[
'id' => 1,
'val' => (object) ['val' => 'x']
],
[
'id' => 2,
'val' => (object) ['val' => 'y']
],
[
'id' => 3,
'val' => (object) ['val' => 'z']
],
];
$objects = [
(object) [
'id' => 1,
'val' => 'value',
'sub' => [ 'key' => 'val'],
],
(object) [
'id' => 2,
'val' => 'human blood',
'sub' => [ 'key' => 'still beating heart'],
],
];
$instances = [
new A(1000),
new A(2000),
new A(3000),
new A(4000),
];
$access = new Atrox\Access;
array_map($access['id'], $arrays) === [1,2,3];
array_map($access['val']->val, $arrays) === ['x','y','z'];
array_map($access->id, $objects) === [1,2];
array_map($access->sub['key'], $objects) === ['val', 'still beating heart'];
array_map($access->i, $instances) === [1000, 2000, 3000, 4000];
array_map($access->mm(1), $instances) === [1001, 2001, 3001, 4001];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment