PHP code example of tuupola / witchcraft

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

    

tuupola / witchcraft example snippets


class Unicorn
{
    private $color;
    private $birthday;

    public function __construct($color = "white", $birthday = null)
    {
        $this->color = $color;
        $this->birthday = $birthday;
    }

    public function getColor()
    {
        return $this->color;
    }

    public function setColor($color)
    {
        $this->color = $color;
        return $this;
    }

    public function getBirthday()
    {
        return $this->birthday;
    }

    public function setBirthday($birthday)
    {
        $this->birthday = DateTime::createFromFormat("Y-m-d", $birthday);
        return $this;
    }

    public function getAge()
    {
        $now = new DateTime();
        return $this->birthday->diff($now)->format("%y years");
    }
}

$unicorn = new Unicorn();
$unicorn->setBirthday("1930-24-12")->setColor("rainbow");
print $unicorn->getAge();

class Unicorn
{
    use \Witchcraft\MagicMethods;

    /* Rest of the code stays exactly the same. */
}

$unicorn = new Unicorn();
$unicorn->birthday("1930-24-12")->color("rainbow");
print $unicorn->age();

class Unicorn
{
    use \Witchcraft\MagicProperties;

    /* Rest of the code stays exactly the same. */
}

$unicorn = new Unicorn();
$unicorn->birthday = "1930-24-12";
$unicorn->color = "rainbow";
print $unicorn->age;

$unicorn->something(function ($input) {
    return "Got {$input}!";
});

$unicorn->something("milk");

/* Got milk! */

$unicorn->something = function ($input) {
    return "No {$input} :(";
};

$unicorn->something("beer");

/* No beer :() */