PHP code example of mcannucci / aspect-override

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

    

mcannucci / aspect-override example snippets




verride\Facades\AspectOverride::initialize(
    AspectOverride\Core\Configuration::create()
        ->setDirectories([
            __DIR__ . '/../app'
        ])
        ->setExcludedDirectories([
            __DIR__ . '/../app/excluded'
        ])
);

use AspectOverride\Override;

class MyClass {
   public function myMethod() {
      return false;
   }
   public static function myStaticMethod() {
      return false;
   }
}

// for any instance of 'MyClass', return true for the method 'myMethod' and 'myStaticMethod' instead of false
Override::method(MyClass::class, 'myMethod', function(){
  return true;
});

Override::method(MyClass::class, 'myStaticMethod', function(){
  return true;
});

// Will work if it's a static or instantiated method
MyClass::myStaticMethod() // true;
(new MyClass)->myMethod(); // true;

use AspectOverride\Override;

class MyClass {
   public static function echoThis(int $a) {
      echo $a;
   }
}

// Before the function 'echoThis' runs we change $a to be incremented
Override::method(MyClass::class, 'echoThis', function(int $a){
  return [$a + 1]
});

MyClass::echoThis(2) // 3;

use AspectOverride\Override;

class MyClass {
   public static function echoSecondArg(int $a, int $b) {
      echo $b;
   }
}

// Before the function 'echoSecondArg' runs we only modify the second argument and keep the first one as is
Override::before(MyClass::class, 'echoSecondArg', function(int $a, int $b){
  return ['b' => $b + 1]
});

MyClass::echoSecondArg(2,3) // 4;

use AspectOverride\Override;

class MyClass {
   public static function echoOne() {
      echo 1;
   }
}

// After the function 'echoOne' runs, we increment the result by one 
Override::after(MyClass::class, 'echoOne', function($a){
  return $a + 1;
});

MyClass::echoOne() // 2;

use AspectOverride\Override;

Override::function('time', function(){
  return 1000;
});

time() // 1000;