PHP code example of solidworx / aspect

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

    

solidworx / aspect example snippets




#[Memoize]
function expensiveComputation(int $x): int
{
    // Simulate a time-consuming operation
    sleep(2);
    return $x * $x;
}

$result = expensiveComputation(5); // Takes 2 seconds
$result = expensiveComputation(5); // Returns instantly from cache



#[Memoize]
function fibonacci(int $n): int
{
    if ($n <= 1) {
        return $n;
    }
    return fibonacci($n - 1) + fibonacci($n - 2);
}

echo fibonacci(10); // Outputs: 55



class DataFetcher
{
    #[Memoize]
    public function fetchData(string $url): array
    {
        // Simulate an API call
        sleep(3);
        return ['data' => 'Sample data from ' . $url];
    }
}

$fetcher = new DataFetcher();
$data = $fetcher->fetchData('https://api.example.com'); // Takes 3 seconds
$data = $fetcher->fetchData('https://api.example.com'); // Returns instantly from cache



#[Memoize]
function multiply(int $a, int $b): int
{
    // Simulate a computation
    sleep(1);
    return $a * $b;
}

echo multiply(2, 3); // Takes 1 second
echo multiply(3, 2); // Takes 1 second (different arguments)
echo multiply(2, 3); // Returns instantly from cache