PHP code example of princejohnsantillan / reflect

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

    

princejohnsantillan / reflect example snippets


enum Plan: string{
    case FREE = 'free';
    case HOBBY = 'hobby';
    case PRO = 'professional';
    case TEAM = 'team';
    case ENTERPRISE = 'enterprise';
    
    public function price(): int {
        return match($this){
            static::FREE => 0,
            static::HOBBY => 10,
            static::PRO => 20,
            static::TEAM => 50,
            static::ENTERPRISE = 200
        };
    }
    
    public function color(): string {
        return match($this){
            static::FREE => 'yellow',
            static::HOBBY => 'orange',
            static::PRO => 'blue',
            static::TEAM => 'silver',
            static::ENTERPRISE = 'gold'
        };
    }
}


use PrinceJohn\Reflect\Traits\HasEnumTarget;

#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
class Price{
    use HasEnumTarget;
    
    public function __construct(public int $price) {}
}

use PrinceJohn\Reflect\Traits\HasEnumTarget;

#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
class Color{
    use HasEnumTarget;
    
    public function __construct(public string $color) {}
}


use PrinceJohn\Reflect\Enum\Reflect;

enum Plan: string{
    #[Price(0)]
    #[Color('yellow')]
    case FREE = 'free';
    
    #[Price(10)]
    #[Color('orange')]
    case HOBBY = 'hobby';
    
    #[Price(20)]
    #[Color('blue')]
    case PRO = 'professional';
    
    #[Price(50)]
    #[Color('silver')]
    case TEAM = 'team';
    
    #[Price(200)]
    #[Color('gold')]
    case ENTERPRISE = 'enterprise';
    
    public function price(): int {
        // Demonstrating usage via the Reflect class        
        return Reflect::on($this)
            ->getAttributeInstance(Price::class)
            ->price;            
    }
    
    public function color(): string {
        // Demonstrating usage via the HasEnumTarget trait
        return Color::onEnum($this)->color;
    }
}