PHP code example of webhappens / magic-properties

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

    

webhappens / magic-properties example snippets


use \WebHappens\MagicProperties\MagicProperties;

public function __call($method, $arguments)
{
    // ...

    if ($property = $this->matchMagicProperty($name)) {
        return $this->callMagicProperty($property, $arguments);
    }

    // ...

    // throw new \BadMethodCallException();
}

$person = new class {
    use MagicProperties;

    public $name = 'Sam';
    protected $role = 'developer';
};

$name = $person->name;
// or
$name = $person->name();

$role = $person->role;
// or
$role = $person->role();

$person->getName();

$person = new class {
    use MagicProperties;

    public $name;
    protected $role;
};

$person->name = 'Sam';
// or
$person->name('Sam');

$person->role = 'developer';
// or
$person->role('developer');

$person->setName('Sam');

protected $readonly = ['id'];

$person->name('Sam')->role('developer');

$person = new class {
    use MagicProperties;

    protected $role = 'developer';

    protected function getRoleProperty($value)
    {
        return ucwords($value);
    }
};

$role = $person->role;
// or
$role = $person->role();

$person = new class {
    use MagicProperties;

    protected $role;

    protected function setRoleProperty($value)
    {
        return ucwords($value);
    }
};

$person->role = 'developer';
// or
$person->role('developer');