PHP 匿名类
Posted by 撒得一地 on 2016年7月15日 in PHP笔记
上一篇: PHP单例模式与工厂模式比较
下一篇: PHP Trait 特性的作用
下一篇: PHP Trait 特性的作用
何为匿名类?
匿名类是不能有名字的类,它们不能被引用,只能在创建时用New语句来声明它们。
正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写。
PHP 7 开始支持匿名类。 匿名类很有用,可以创建一次性的简单对象。比如:
<?php // PHP 7 之前的代码 class Logger { public function log($msg) { echo $msg; } } $util->setLogger(new Logger()); // 使用了 PHP 7+ 后的代码 $util->setLogger(new class { public function log($msg) { echo $msg; } });
在 PHP7 匿名类中,不仅可以传递参数到匿名类的构造器,也可以扩展(extend)其他类、实现接口(implement interface),以及像其他普通的类一样使用 trait:
<?php class SomeClass {} interface SomeInterface {} trait SomeTrait {} var_dump(new class(10) extends SomeClass implements SomeInterface { private $num; public function __construct($num) { $this->num = $num; } use SomeTrait; });
以上例程会输出:
object(class@anonymous)#1 (1) { ["Command line code0x104c5b612":"class@anonymous":private]=> int(10) }
PHP 匿名类被嵌套进普通 Class 后,不能访问这个外部类(Outer class)的 private(私有)、protected(受保护)方法或者属性。 为了访问外部类(Outer class)protected 属性或方法,PHP 匿名类可以 extend(扩展)此外部类。 为了使用外部类(Outer class)的 private 属性,必须通过构造器传进来:
<?php class Outer { private $prop = 1; protected $prop2 = 2; protected function func1() { return 3; } public function func2() { return new class($this->prop) extends Outer { private $prop3; public function __construct($prop) { $this->prop3 = $prop; } public function func3() { return $this->prop2 + $this->prop3 + $this->func1(); } }; } } echo (new Outer)->func2()->func3();
以上例程会输出:
6
以下列举了三个关于 PHP 匿名类的例子,不仅非常基础而且易于理解:
<?php // First way - anonymous class assigned directly to variable //第一种方式,为匿名类直接分配参数 $ano_class_obj = new class{ public $prop1 = 'hello'; public $prop2 = 754; const SETT = 'some config'; public function getValue() { // do some operation return 'some returned value'; } public function getValueWithArgu($str) { // do some operation return 'returned value is '.$str; } }; echo "\n"; var_dump($ano_class_obj); echo "\n"; echo $ano_class_obj->prop1; echo "\n"; echo $ano_class_obj->prop2; echo "\n"; echo $ano_class_obj::SETT; echo "\n"; echo $ano_class_obj->getValue(); echo "\n"; echo $ano_class_obj->getValueWithArgu('OOP'); echo "\n"; // Second way - anonymous class assigned to variable via defined function //第二种方式,在自定义函数内将生成的匿名类作为结果返回 $ano_class_obj_with_func = ano_func(); function ano_func() { return new class { public $prop1 = 'hello'; public $prop2 = 754; const SETT = 'some config'; public function getValue() { // do some operation return 'some returned value'; } public function getValueWithArgu($str) { // do some operation return 'returned value is '.$str; } }; } echo "\n"; var_dump($ano_class_obj_with_func); echo "\n"; echo $ano_class_obj_with_func->prop1; echo "\n"; echo $ano_class_obj_with_func->prop2; echo "\n"; echo $ano_class_obj_with_func::SETT; echo "\n"; echo $ano_class_obj_with_func->getValue(); echo "\n"; echo $ano_class_obj_with_func->getValueWithArgu('OOP'); echo "\n"; echo "\n"; // Third way - passing argument to anonymous class via constructors 第三种方式,传递参数给匿名类构造函数 $arg = 1; // we got it by some operation $config = [2, false]; // we got it by some operation $ano_class_obj_with_arg = ano_func_with_arg($arg, $config); function ano_func_with_arg($arg, $config) { return new class($arg, $config) { public $prop1 = 'hello'; public $prop2 = 754; public $prop3, $config; const SETT = 'some config'; public function __construct($arg, $config) { $this->prop3 = $arg; $this->config =$config; } public function getValue() { // do some operation return 'some returned value'; } public function getValueWithArgu($str) { // do some operation return 'returned value is '.$str; } }; } echo "\n"; var_dump($ano_class_obj_with_arg); echo "\n"; echo $ano_class_obj_with_arg->prop1; echo "\n"; echo $ano_class_obj_with_arg->prop2; echo "\n"; echo $ano_class_obj_with_arg::SETT; echo "\n"; echo $ano_class_obj_with_arg->getValue(); echo "\n"; echo $ano_class_obj_with_arg->getValueWithArgu('OOP'); echo "\n"; echo "\n";
上一篇: PHP单例模式与工厂模式比较
下一篇: PHP Trait 特性的作用
下一篇: PHP Trait 特性的作用