PHP code example of athwari / laravel-method-overrider

1. Go to this page and download the library: Download athwari/laravel-method-overrider 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/ */

    

athwari / laravel-method-overrider example snippets


return [
    'ignore_final_methods' => true,
];

use Athwari\MethodOverrider\Facades\MethodOverrider;

class TestService
{
    public function greet(string $name): string
    {
        return "Hello {$name}";
    }
}

$service = MethodOverrider::override(
    TestService::class,
    'greet',
    function ($original, $name) {
        return strtoupper($original($name));
    }
);

echo $service->greet('Taylor'); // HELLO TAYLOR

$service = MethodOverrider::override(
    TestService::class,
    ['greet', 'nullable'],
    [
        function ($original, $name) {
            return strtoupper($original($name));
        },
        function ($original, $name) {
            return $original($name);
        },
    ]
);
bash
php artisan vendor:publish --tag=method-overrider-config