Errors in PHP

In PHP, error handling is typically done using exceptions, which is different from Go’s approach. However, we can still demonstrate similar concepts using PHP’s error handling mechanisms.

<?php

// In PHP, we don't need to declare a package or import statements.

// We'll use exceptions for error handling in PHP.
use Exception;

/**
 * This function demonstrates error handling similar to Go's approach.
 * It returns an array with the result and any error that occurred.
 */
function f($arg) {
    if ($arg === 42) {
        // In PHP, we throw exceptions instead of returning errors.
        return [null, new Exception("can't work with 42")];
    }
    // Return the result and null for no error.
    return [$arg + 3, null];
}

// In PHP, we can define custom exception classes for specific error types.
class OutOfTeaException extends Exception {}
class PowerException extends Exception {}

/**
 * This function demonstrates using custom exceptions.
 */
function makeTea($arg) {
    if ($arg === 2) {
        throw new OutOfTeaException("no more tea available");
    } elseif ($arg === 4) {
        // In PHP, we can't directly wrap exceptions, but we can catch and rethrow
        throw new Exception("making tea: " . (new PowerException("can't boil water"))->getMessage());
    }
    return null;
}

// Main execution
try {
    foreach ([7, 42] as $i) {
        [$result, $error] = f($i);
        if ($error !== null) {
            echo "f failed: " . $error->getMessage() . "\n";
        } else {
            echo "f worked: $result\n";
        }
    }

    for ($i = 0; $i < 5; $i++) {
        try {
            makeTea($i);
            echo "Tea is ready!\n";
        } catch (OutOfTeaException $e) {
            echo "We should buy new tea!\n";
        } catch (PowerException $e) {
            echo "Now it is dark.\n";
        } catch (Exception $e) {
            echo "unknown error: " . $e->getMessage() . "\n";
        }
    }
} catch (Exception $e) {
    echo "An unexpected error occurred: " . $e->getMessage() . "\n";
}

In PHP, error handling is typically done using exceptions. Here’s how the concepts translate:

  1. Instead of returning errors, PHP throws exceptions.
  2. The error interface in Go is similar to PHP’s Exception class.
  3. PHP doesn’t have a built-in way to wrap errors like Go’s fmt.Errorf with %w, but we can achieve similar functionality by catching and rethrowing exceptions with additional context.
  4. PHP’s try-catch blocks are used for error handling, similar to Go’s if err != nil checks.
  5. Custom error types in Go can be replicated in PHP by creating custom exception classes.

To run this PHP script:

$ php errors.php
f worked: 10
f failed: can't work with 42
Tea is ready!
Tea is ready!
We should buy new tea!
Tea is ready!
Now it is dark.

This example demonstrates how to handle errors in PHP, create custom exceptions, and use try-catch blocks for error handling. While the approach is different from Go, the core concepts of explicit error handling and custom error types are still present.