match表达式
<?php
trait
<?php
trait Dog{
public $name = "dog";
public static $static = 'foo';
public function drive(){
echo "This is dog drive";
}
public function eat(){
echo "This is dog eat";
}
public static function body() :void {
echo "This is dog body";
}
abstract public function getName();
}
class Cat{
use Dog;
public function drive(){
echo "This is cat drive";
}
public function getName(){
echo "This is Cat name";
}
}
$cat = new Cat();
预定义接口之 ArrayAccess
<?php
class Obj implements ArrayAccess {
private $container = array();
public function __construct() {
$this->container = array(
"one" => 1,
"two" => 2,
"three" => 3,
);
}
public function offsetSet($offset, $value): void {
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset): bool {
return isset($this->container[$offset]);
}
public function offsetUnset($offset): void {
unset($this->container[$offset]);
}
public function offsetGet($offset): mixed {
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}
$obj = new Obj;
class Obj2{
public $data;
public function __construct() {
$this->data = array(
"one" => 1,
"two" => 2,
"three" => 3,
);
}
public function __get($key): mixed
{
return $this->data[$key];
}
public function __set($key, $val): void
{
$this->data[$key] = $val;
}
public function __isset($key): bool
{
return isset($this->data[$key]);
}
public function __unset($key): void
{
unset($this->data[$key]);
}
}
$obj2 = new Obj2;
$obj2->one = "A value";
$obj2->Append1 = 'Append 1';
$obj2->Append2 = 'Append 2';
$obj2->Append3 = 'Append 3';
print_r($obj2);
枚举
Enum 定义
<?php
enum Suit{
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
print Suit::Spades->name;
print Suit::Diamonds->name;
回退(Backed)枚举
<?php
enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
?>
枚举方法
<?php
interface Colorful{
public function color(): string;
}
trait Rectangle
{
public function shape(): string {
return "trait Rectangle";
}
}
enum Suit implements Colorful{
use Rectangle;
case Hearts;
case Diamonds;
case Clubs;
case Spades;
public function color(): string{
return match($this){
Suit::Hearts, Suit::Diamonds => 'Red',
Suit::Clubs, Suit::Spades => 'Black',
};
}
public function SuitShape(): string{
return "SuitShape";
}
}
function paint(Colorful $c) {
echo $c->name;
}
function paint_color(Colorful $c) {
echo $c->color();
}
print serialize(Suit::Hearts);
enum Foo {
case Bar;
}
enum Baz: int {
case Beep = 5;
}
枚举静态方法
<?php
enum Size{
case Small;
case Medium;
case Large;
public const Huge = self::Large;
public static function fromLength(int $cm): static{
return match(true){
$cm < 50 => static::Small,
$cm < 100 => static::Medium,
default => static::Large,
};
}
}
var_dump(Size::Huge);
范例用法
<?php
enum UserStatus: string {
case Pending = 'P';
case Active = 'A';
case Suspended = 'S';
case CanceledByUser = 'C';
public function label(): string
{
return match($this) {
static::Pending => 'Pending',
static::Active => 'Active',
static::Suspended => 'Suspended',
static::CanceledByUser => 'Canceled by user',
};
}
}
foreach (UserStatus::cases() as $case) {
printf('<option value="%s">%s</option>\n', $case->value, $case->label());
}
|