2017 © Pedro Peláez
 

library gears-overloading

Description of project Gears.Overloading.

image

ailixter/gears-overloading

Description of project Gears.Overloading.

  • Wednesday, January 17, 2018
  • by ailixter
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

gears-overloading

install

composer require ailixter/gears-overloading, (*1)

howtos

How to provide getter/setter overriding for properties

use Ailixter\Gears\Props;

class TestProps
{
    use Props;

    private $myPri = 'my private';

    public function getMyPri() {
        return '*' . $this->myPri;
    }
}

$test = new TestProps;
echo $test->myPri;
$test->myPri = 'new'; // PropertyException

or, for real (defined) props:, (*2)

class TestRealProps
{
    use Props;

    private $myPri = 'my private';

    protected function propertyGet($prop)
    {
        return $this->{$prop};
    }

    protected function propertySet($prop, $value)
    {
        $this->{$prop} = $value;
    }
}

$test = new TestRealProps;
echo $test->myPri;
$test->myPri = 'new';
echo $test->undefined;     // Notice
$test->undefined = 'some'; // perfectly ok

How to support explicitely defined properties only

use Ailixter\Gears\StrictProps;

class TestStrictProps
{
    use StrictProps;

    private $myPri = 'my private';
}

$test = new TestStrictProps;
echo $test->myPri;
$test->myPri = 'new';
echo $test->undefined;     // PropertyException
$test->undefined = 'some'; // PropertyException

How to create getters and fluent setters for all defined props

use Ailixter\Gears\AutoGetSetProps;

class TestAutoGetSetProps
{
    use AutoGetSetProps;

    private $myPri;
}

$test = TestAutoGetSetProps;
echo $test->setMyPri('new')->getMyPri();

it's also possible to specify defaults in getters:, (*3)

class TestAutoGetPropsWithDefaults
{
    use Props, AutoGetSetProps;

    private   $myPri;
    protected $myPro;

    public function getMyPro($default = 'static default')
    {
        return $this->existingProperty('myPro', $default);
    }
}

$test = TestAutoGetSetProps;
echo $test->myPro;                       // static default
echo $test->getMyPri('dynamic default'); // dynamic default

How to implement property binding

This enables you to delegate procedural property getting/setting., (*4)

use Ailixter\Gears\BoundProps;

class TestBoundProps
{
    use BoundProps;

    /** @var BoundPropsInterface */
    private $myBoundPri;
}

$test = new TestBoundProps;
$test->myBoundPri = new TestPropsBinding;
$test->myBoundPri = 'new';
echo $test->myBoundPri;

How to proxy your objects

use Ailixter\Gears\AbstractProxy;

class TestProxy extends AbstractProxy {}

class TestObject {
    public $myPub = 'my public';
    public function myFn () {
        return __function__;
    }
}

$test = new TestProxy(new TestObject);
echo $test->myPub;
$test->myPub = 'new';
echo $this->myFn();

or, (*5)

class CustomProxy
{
    use Proxy;

    private $proxiedObject;

    public function __construct($proxiedObject)
    {
        $this->proxiedObject = $proxiedObject;
    }
}

or, (*6)

class LazyProxy
{
    use Proxy;

    private $obj;

    protected function getProxiedObject()
    {
        if (!$this->obj instanceof ProxiedObject) {
            $this->obj = new ProxiedObject;
        }
        return $this->obj;
    }
}

The Versions

17/01 2018

dev-master

9999999-dev

Description of project Gears.Overloading.

  Sources   Download

by Avatar ailixter