PHP code example of spekkionu / property-access

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

    

spekkionu / property-access example snippets


use Spekkionu\PropertyAccess\PropertyAccessTrait;

class ExampleClass
{
    use PropertyAccessTrait;

    private $name;

    private $email;

}


$example = new ExampleClass();

$example->name = 'Bob';
$example->email = '[email protected]';

echo $example->name; // Bob

$example->fill(array(
    'name' => 'Steve',
    'email' => '[email protected]'
));

echo $example->email; // [email protected]

use Spekkionu\PropertyAccess\PropertyAccessTrait;

class ExampleClass
{
    use PropertyAccessTrait;

    private $name;

    private $email;

    public function setEmail(EmailAddress $email){
        $this->email = $email;
    }

}

// Value Object
class EmailAddress
{
    private $email;

    public function __construct($email)
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new InvalidArgumentException('Not a valid email address.');
        }
        $this->email = $email;
    }

    public function getValue()
    {
        return $this->email;
    }

    public function __toString()
    {
        return $this->getValue();
    }
}

// Usage
$example = new ExampleClass();
$example->email = new EmailAddress('[email protected]');