PHP code example of imponeer / object-errors

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

    

imponeer / object-errors example snippets


use Imponeer\ObjectErrors\ErrorsCollection;

class MyObject {
    /**
     * @var ErrorsCollection|null
     */
    public $errors = null;

    public function __construct() {
        $this->errors = new ErrorsCollection();
    }

    public function doSomething() {
        // Example logic
        if ($failed) {
            $this->errors->add("Some error");
        }
    }

    public function render() {
        if ($this->errors->isEmpty()) {
            return 'Everything fine';
        } else {
            return $this->errors->getHtml();
        }
    }
}

use Imponeer\ObjectErrors\ErrorsTrait;

class MyObject {
    use ErrorsTrait;

    public function doSomething() {
        if ($failed) {
            $this->setErrors("Some error");
        }
    }

    public function render() {
        if ($this->hasError()) {
            return $this->getHtmlErrors();
        }
        return 'Everything fine';
    }
}