PHP code example of pulyaevskiy / enum

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

    

pulyaevskiy / enum example snippets



namespace Acme\Example;

use Pulyaevskiy\Enum;

/**
 * @method static Shape triangle()
 * @method static Shape square()
 * @method static Shape pentagon()
 * @method static Shape hexagon()
 */
class Shape extends Enum 
{
    const TRIANGLE = 'triangle';
    const SQUARE = 'square';
    const PENTAGON = 'pentagon';
    const HEXAGON = 'hexagon';
}


namespace Acme\Example;

class Model 
{
    /** @var Shape */
    private $shape;

    public function setShape(Shape $value)
    {
        // Here it is guaranteed that the $value has already been validated
        $this->shape = $value;
    }
}


namespace Acme\Example;

class Application 
{
    public function changeShapeToSquare(Model $model)
    {
        // Enum class has __callStatic method that checks if there is constant with name of called function
        // and then create instance of Shape class with this value
        $shape = Shape::square();
        $model->setShape($shape);
    }
}