-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessor.php
More file actions
42 lines (33 loc) · 1.27 KB
/
Accessor.php
File metadata and controls
42 lines (33 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php declare(strict_types=1);
namespace PiotrPress;
use ReflectionClass;
class Accessor {
protected $object = null;
protected $class = null;
public function __construct( $object ) {
$this->object = $object;
$this->class = new ReflectionClass( $object );
}
public function __call( string $name, array $args = [] ) {
$method = $this->method( $name );
return $method->isStatic() ? $method->invokeArgs( null, $args ) : $method->invokeArgs( $this->object, $args );
}
public function __get( string $name ) {
$property = $this->property( $name );
return $property->isStatic() ? $property->getValue( null ) : $property->getValue( $this->object );
}
public function __set( string $name, $value ) {
$property = $this->property( $name );
$property->isStatic() ? $property->setValue( $value ) : $property->setValue( $this->object, $value );
}
protected function method( string $name ) {
$method = $this->class->getMethod( $name );
$method->setAccessible( true );
return $method;
}
protected function property( string $name ) {
$property = $this->class->getProperty( $name );
$property->setAccessible( true );
return $property;
}
}