PHP code example of lhellemons / php-value-objects

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

    

lhellemons / php-value-objects example snippets




use SolidPhp\ValueObjects\Enum\EnumTrait;

final class Weekday
{
    use EnumTrait;

    public static function MONDAY(): self
    {
        return self::define('MONDAY');
    }

    public static function TUESDAY(): self
    {
        return self::define('TUESDAY');
    }

    // ...
}

// ...

$monday = Weekday::MONDAY();
$tuesday = Weekday::TUESDAY();
$deliveryDay = WeekDay::MONDAY();

$monday === $deliveryDay; // true
$monday === $tuesday; // false


use SolidPhp\ValueObjects\Value\ValueObjectTrait;

final class EmailAddress
{
    use ValueObjectTrait;

    /** @var string */
    private $emailAddressString;

    private function __construct(string $emailAddressString)
    {
        $this->emailAddressString = $emailAddressString;
    }

    public function of(string $emailAddressString): self
    {
        $normalizedString = strtolower(trim($emailAddressString));
        return static::getInstance($normalizedString);
    }

    public function getString(): string
    {
        return $this->emailAddressString;
    }
}

// ...

$emailAddress = EmailAddress::of("[email protected]");
$sameEmailAddress = EmailAddress::of(" [email protected]");

$emailAddress === $sameEmailAddress; // true