php匿名类的应用场景,【modernPHP专题(9)】匿名类
類結構
Closure {
/* 方法 */
// 用于禁止實例化的構造函數
__construct ( void )
// 復制一個閉包,綁定指定的$this對象和類作用域。
public static Closure bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] )
// 復制當前閉包對象,綁定指定的$this對象和類作用域。
public Closure bindTo ( object $newthis [, mixed $newscope = 'static' ] )
}
// 類作用域,可以是對象,也可以是實例名稱
什么是匿名類?
先理解以下三個例子
例1. 閉包函數都是繼承Closure類
class A {
public static function testA() {
return function($i) { //返回匿名函數
return $i+100;
};
}
}
function B(Closure $callback)
{
return $callback(200);
}
$a = B(A::testA());
// A::testA() 返回匿名函數,也就是閉包函數,所有閉包函數都是繼承Closure類
// print_r($a); //輸出 300
例2. 將一個匿名函數綁定到一個類中。返回Closure類
class A {
public $base = 100;
public function funca()
{
echo 2222;
}
}
class B {
private $base = 1000;
}
// bind : 復制一個閉包,綁定指定的$this對象和類作用域。
$f = function () {
return $this->base + 3;
};
print_r($f);
/**
* $f其實就是一個closure對象
Closure Object
(
)
*/
$a = Closure::bind($f, new A);
print_r($a());//out: 103
print_r($a);
/*
* out:
Closure Object
(
[this] => A Object
(
[base] => 100
)
)
*/
// 第三個參數就聲明了這個函數的可調用范圍(如果該函數要調用private)
, 該參數可以是對象實例,也可以是類名
$b = Closure::bind($f, new B, 'B');
print_r($b);
/**
* out:
Closure Object
(
[this] => B Object
(
[base:B:private] => 1000
)
)
*/
print_r($b());//out: 1003
3. 第二參數為null,代表靜態調用static
class A {
private static $sfoo = 1;
private $ifoo = 2;
}
// 要調靜態的屬性,就必須聲明static
$cl1 = static function() {
return A::$sfoo;
};
$cl2 = function() {
return $this->ifoo;
};
// 第二參數為null,就代表調用static
$bcl1 = Closure::bind($cl1, null, 'A');
$bcl2 = Closure::bind($cl2, new A(), 'A');
// 以closure對象調用靜態屬性
$bcl3 = $cl1->bindTo(null,'A');
echo $bcl1(), "\n";//輸出 1
echo $bcl2(), "\n";//輸出 2
echo $bcl3(); // 輸出1
匿名類有什么用?
給類動態添加新方法
trait DynamicTrait {
/**
* 自動調用類中存在的方法
*/
public function __call($name, $args) {
if(is_callable($this->$name)){
return call_user_func($this->$name, $args);
}else{
throw new \RuntimeException("Method {$name} does not exist");
}
}
/**
* 添加方法
*/
public function __set($name, $value) {
$this->$name = is_callable($value)?
$value->bindTo($this, $this):
$value;
}
}
/**
* 只帶屬性不帶方法動物類
*
* @author fantasy
*/
class Animal {
use DynamicTrait;
private $dog = '汪汪隊';
}
$animal = new Animal;
// 往動物類實例中添加一個方法獲取實例的私有屬性$dog
$animal->getdog = function() {
return $this->dog;
};
echo $animal->getdog();//輸出 汪汪隊
模板渲染輸出
Template.php
class Template{
/**
* 渲染方法
*
* @access public
* @param obj 信息類
* @param string 模板文件名
*/
public function render($context, $tpl){
$closure = function($tpl){
ob_start();
include $tpl;
return ob_end_flush();
};
// PHP7: $closure->call($context, $tpl);
$closure = $closure->bindTo($context, $context);
$closure($tpl);
}
}
Article.php
/**
* 文章信息類
*/
class Article
{
private $title = "這是文章標題";
private $content = "這是文章內容";
}
tpl.php
···
···
<?php echo $this->title;?>
<?php echo $this->content;?>
···
···
index.php
function __autoload($class) {
require_once "$class.php";
}
$template = new Template;
$template->render(new Article, 'tpl.php');
PHP7 新增的call方法
class Value
{
protected $value;
public function __construct($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
$three = new Value(3);
$four = new Value(4);
$closure = function ($delta){
return $this->getValue() + $delta;
};
/**
* function call ($newThis, ...$parameters)
* 把$closure綁定到$three,并調用;第二參數起就是閉包的參數
*/
echo $closure->call($three , 3);
echo PHP_EOL;
echo $closure->call($four , 4);
總結
以上是生活随笔為你收集整理的php匿名类的应用场景,【modernPHP专题(9)】匿名类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4k对齐选多少?
- 下一篇: php 输出01,php基础01_thi