PHP code example of ailixter / gears-overloading

1. Go to this page and download the library: Download ailixter/gears-overloading library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ailixter / gears-overloading example snippets


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

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

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

use Ailixter\Gears\AutoGetSetProps;

class TestAutoGetSetProps
{
    use AutoGetSetProps;

    private $myPri;
}

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

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

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;

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();

class CustomProxy
{
    use Proxy;

    private $proxiedObject;

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

class LazyProxy
{
    use Proxy;

    private $obj;

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