PHP code example of timefrontiers / php-instance-error

1. Go to this page and download the library: Download timefrontiers/php-instance-error 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/ */

    

timefrontiers / php-instance-error example snippets


[min_rank, code, message, file, line]
// Example:
[0, 256, 'Invalid email format', '/app/User.php', 42]

use TimeFrontiers\InstanceError;

// Extract errors visible to current user (uses global $session)
$extractor = new InstanceError($userObject);
$errors = $extractor->get();

// Get errors for a specific context
$loginErrors = $extractor->get('login');

// Get only error messages (strings)
$messages = $extractor->get('login', true);

use TimeFrontiers\InstanceError;
use TimeFrontiers\AccessRank;

// Show ALL errors regardless of rank
$extractor = new InstanceError($object, true);

// Show errors visible to DEVELOPER rank
$extractor = new InstanceError($object, AccessRank::DEVELOPER);

// Show errors visible to rank 5
$extractor = new InstanceError($object, 5);

$extractor = new InstanceError($object);

// Check if errors exist
if ($extractor->has('validation')) {
  // ...
}

// Count visible errors
$count = $extractor->count();
$validationCount = $extractor->count('validation');

// Get first error message
$first = $extractor->first();

// Get all messages as flat array
$allMessages = $extractor->messages();

$extractor = new InstanceError($object, true);

// Log all errors
$extractor->log();

// Log specific context
$extractor->log('database');

// Log to specific file
$extractor->log('database', '/var/log/app/db-errors.log');

$extractor = new InstanceError($object);

// Add an error to the collection
$extractor->put('custom', [
  AccessRank::GUEST->value,  // min_rank
  400,                        // code
  'Something went wrong',     // message
  __FILE__,                   // file
  __LINE__                    // line
]);

use TimeFrontiers\Helper\HasErrors;

class MyService {
  use HasErrors;

  public function process():bool {
    if ($error) {
      $this->_userError('process', 400, 'Processing failed');
      return false;
    }
    return true;
  }
}

// Extract errors
$service = new MyService();
$service->process();

$extractor = new InstanceError($service);
$errors = $extractor->get('process');
bash
composer