PHP code example of benrowe / properties

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

    

benrowe / properties example snippets

 php
class Sample extends AbstractBase
{
    public function __construct()
    {
        // do stuff
        $this->configureProperties();
        // properties now available
    }

    /**
     * This method is automatically called by AbstractBase once
     * the constructor has finished executing
     */
    public function configureProperties()
    {
        $this->
            addProperty('simple')

        $this
            ->addProperty('complex', 'string', 'default value')
            ->setter(function($value) {
                return trim($value);
            })
            ->getter(function($value) {
                return str_reverse($value);
            })
            ->validate(function ($value) {
                return strlen($value) > 0;
            });

    }
}

$sample = new Sample;
$sample->simple = 'test';
$sample->undefined = 'newval'; // throws PropertyException "Undefined property 'undefined'"
$sample->complex; // 'default value'
$sample->complex = ''; // throws PropertyException "property 'complex' invalid value on set"
$sample->complex = ' hello world';
echo $sample->complex; //'dlrow olleh';