PHP code example of gmazzap / andrew

1. Go to this page and download the library: Download gmazzap/andrew 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/ */

    

gmazzap / andrew example snippets


class Subject
{
  private $var = 'I am private';
  
  public function getVar() {
      return $this->var;
  }
  
  public function hasVar() {
      return isset($this->var);
  }
  
  private function testMe() {
      return 'I am private';
  }
}

$subject = new Subject();

$proxy = new Andrew\Proxy($subject);

// get subject private var via proxy
echo $proxy->var; // 'I am private'

// set subject private var via proxy
$proxy->var = 'I WAS private';
echo $subject->getVar(); // 'I WAS private'

// check subject private var is set via proxy
isset($proxy->var); // true

// unset subject private var via proxy
unset($proxy->var);
$subject->hasVar(); // false

// call subject private method via proxy
echo $proxy->testMe() // 'I am private'

class Subject
{
    private static $var = 'I am private static';
  
    public static function getVar() {
        return self::$var;
    }
  
    private static function testMe() {
        return 'I am private static';
    }
}

$proxy = new Andrew\StaticProxy(Subject::class); // we pass class name, not object

// getter
echo $proxy->var; // 'I am private static'

// setter
$proxy->var = 'I WAS private static';
echo Subject::getVar(); // 'I WAS private static'

// isser
isset($proxy->var); // true

// caller
echo $proxy->testMe() // 'I am private static'