note 125325 added to language.oop5.overloading
| From: | elCreator@php.net | Date: | Thu, 03 Sep 2020 23:06:08 +0000 |
| Subject: | note 125325 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215067@lists.php.net to get a copy of this message | ||
class Enum {
protected string $_value;
protected function __construct(string $value) {
$this->_value = $value;
}
public function is($key)
{
return $this->_value === $key;
}
public static function __callStatic($name, $params) {
$value = constant("static::$name");
if (!$value) {
throw new \InvalidArgumentException(static::class . " can't be $name");
}
return new static($value);
}
public function __toString() {
return $this->_value;
}
}
class Season extends Enum {
public const WINTER = 'winter';
public const SPRING = 'spring';
public const SUMMER = 'summer';
public const AUTUMN = 'autumn';
}
$now = Season::AUTUMN(); // Autocomplete works as Season::AUTUMN exists
var_export($now->is(Season::AUTUMN)); // true
var_export("$now" === Season::AUTUMN); // true
var_export($now == Season::AUTUMN); // true
var_export($now == Season::SPRING); // false
echo "$now"; // autumn
----
Server IP: 45.112.84.4
Probable Submitter: 103.51.167.36 (proxied: 77.120.176.83)
----
Manual Page -- http://php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/125325
Del: integrated -- https://master.php.net/note/delete/125325/integrated
Del: useless -- https://master.php.net/note/delete/125325/useless
Del: bad code -- https://master.php.net/note/delete/125325/bad+code
Del: spam -- https://master.php.net/note/delete/125325/spam
Del: non-english -- https://master.php.net/note/delete/125325/non-english
Del: in docs -- https://master.php.net/note/delete/125325/in+docs
Del: other reasons-- https://master.php.net/note/delete/125325
Reject -- https://master.php.net/note/reject/125325
Search -- https://master.php.net/manage/user-notes.php