PHP code example of thinktomorrow / magic-attributes

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

    

thinktomorrow / magic-attributes example snippets


if(!isset($class->foo)) return null;
if(!isset($class->foo->bar)) return null;

return $class->foo->bar;

return $class->attr('foo.bar');


use \Thinktomorrow\MagicAttributes\HasMagicAttributes;

class Customer{
    use HasMagicAttributes;
}



class Customer{

    /* allows for $customer->addressStreet instead of $customer->attr('address.street') */
    public function __get($key)
    {
        return $this->magicAttribute($key);
    }

    /* with __isset you allow to check if the property exists on this class, e.g. isset($customer->addressStreet) */
    public function __isset($key)
    {
        return false !== $this->magicAttribute($key, false);
    }

}