在构造函数中添加闭包是好习惯吗?
Is it good pratice to add closure inside constructor?
我在构造函数中添加了闭包,但不确定其性能和最佳实践。如果这样做不正确,最好的方法是什么?
<?php
class Foo
{
private $operation;
public function __construct(){
$this->operation = [
"==" => function($a,$b) {return $a == $b;}
];
}
public function processComparision($operand1,$operator,$operand2){
if($operator == "=="){
$func = $this->operation[$operator];
return $func($operand1,$operand2);
}
}
}
?>
我有一个 own array class,在构造函数中定义了很多函数(或闭包)。这种类型的函数定义还允许您使用方法添加自己的函数。
public function addSqlFunction($name, $function){
$this->userFct[$name] = $function;
return $this;
}
我只看到这样做的好处。我对速度也很满意。因此,这对我来说是“最佳实践”。
我在构造函数中添加了闭包,但不确定其性能和最佳实践。如果这样做不正确,最好的方法是什么?
<?php
class Foo
{
private $operation;
public function __construct(){
$this->operation = [
"==" => function($a,$b) {return $a == $b;}
];
}
public function processComparision($operand1,$operator,$operand2){
if($operator == "=="){
$func = $this->operation[$operator];
return $func($operand1,$operand2);
}
}
}
?>
我有一个 own array class,在构造函数中定义了很多函数(或闭包)。这种类型的函数定义还允许您使用方法添加自己的函数。
public function addSqlFunction($name, $function){
$this->userFct[$name] = $function;
return $this;
}
我只看到这样做的好处。我对速度也很满意。因此,这对我来说是“最佳实践”。