- var_dump: documentation ( source)
- in_array: documentation ( source)
<?php
declare(strict_types = 1);
namespace App
{
class Status
{
const ACTIVE = 1;
const INACTIVE = 2;
const SOLD = 3;
private $value;
private $labels = [
self::ACTIVE => 'Active',
self::ACTIVE => 'Inactive',
self::ACTIVE => 'Sold',
];
/**
* Status constructor.
* @param $value
*/
public function __construct(int $value)
{
if (in_array($value, $this->labels)) {
throw new \InvalidArgumentException('Invalid status');
}
$this->value = $value;
}
public function __toString()
{
return $this->labels[$this->value];
}
}
class Product
{
private $id;
private $name;
private $status;
/**
* Product constructor.
* @param $id
* @param $name
* @param $status
*/
public function __construct(int $id, string $name, Status $status)
{
$this->id = $id;
$this->name = $name;
$this->status = $status;
}
}
var_dump(new Product(1, 'Chess board', new Status(Status::SOLD)));
}