diff --git a/docs/api/framework/base/Object.md b/docs/api/framework/base/Object.md index d61486d..6764504 100644 --- a/docs/api/framework/base/Object.md +++ b/docs/api/framework/base/Object.md @@ -34,6 +34,3 @@ One can call [[hasProperty]], [[canGetProperty]] and/or [[canSetProperty]] to ch Besides the property feature, the Object class defines a static method [[create]] which provides a convenient alternative way of creating a new object instance. - -The Object class also defines the [[evaluateExpression]] method so that a PHP expression or callback can be dynamically -evaluated within the context of an object. \ No newline at end of file diff --git a/framework/base/Component.php b/framework/base/Component.php index 4767803..e8e814b 100644 --- a/framework/base/Component.php +++ b/framework/base/Component.php @@ -92,7 +92,7 @@ class Component extends \yii\base\Object } elseif (strncmp($name, 'as ', 3) === 0) { // as behavior: attach behavior $name = trim(substr($name, 3)); - $this->attachBehavior($name, \Yii::createObject($value)); + $this->attachBehavior($name, $value instanceof Behavior ? $value : \Yii::createObject($value)); } else { // behavior property $this->ensureBehaviors(); diff --git a/framework/base/Object.php b/framework/base/Object.php index 3cd5d12..770234a 100644 --- a/framework/base/Object.php +++ b/framework/base/Object.php @@ -180,41 +180,6 @@ class Object } /** - * Evaluates a PHP expression or callback under the context of this object. - * - * Valid PHP callback can be class method name in the form of - * array(ClassName/Object, MethodName), or anonymous function. - * - * If a PHP callback is used, the corresponding function/method signature should be - * - * ~~~ - * function foo($param1, $param2, ..., $object) { ... } - * ~~~ - * - * where the array elements in the second parameter to this method will be passed - * to the callback as `$param1`, `$param2`, ...; and the last parameter will be the object itself. - * - * If a PHP expression is used, the second parameter will be "extracted" into PHP variables - * that can be directly accessed in the expression. - * See [PHP extract](http://us.php.net/manual/en/function.extract.php) - * for more details. In the expression, the object can be accessed using `$this`. - * - * @param mixed $_expression_ a PHP expression or PHP callback to be evaluated. - * @param array $_data_ additional parameters to be passed to the above expression/callback. - * @return mixed the expression result - */ - public function evaluateExpression($_expression_, $_data_ = array()) - { - if (is_string($_expression_)) { - extract($_data_); - return eval('return ' . $_expression_ . ';'); - } else { - $_data_[] = $this; - return call_user_func_array($_expression_, $_data_); - } - } - - /** * Creates a new instance of the calling class. * * The newly created object will be initialized with the specified configuration. diff --git a/framework/caching/ExpressionDependency.php b/framework/caching/ExpressionDependency.php index 19b3b48..a553cdc 100644 --- a/framework/caching/ExpressionDependency.php +++ b/framework/caching/ExpressionDependency.php @@ -12,9 +12,8 @@ namespace yii\caching; /** * ExpressionDependency represents a dependency based on the result of a PHP expression. * - * ExpressionDependency performs dependency checking based on the - * result of a PHP {@link expression}. - * The dependency is reported as unchanged if and only if the result is + * ExpressionDependency will use `eval()` to evaluate the PHP expression. + * The dependency is reported as unchanged if and only if the result of the expression is * the same as the one evaluated when storing the data to cache. * * @author Qiang Xue @@ -24,10 +23,6 @@ class ExpressionDependency extends Dependency { /** * @var string the PHP expression whose result is used to determine the dependency. - * The expression can also be a valid PHP callback, - * including class method name (array(ClassName/Object, MethodName)), - * or anonymous function (PHP 5.3.0+). The function/method will be passed with a - * parameter which is the dependency object itself. */ public $expression; @@ -47,6 +42,6 @@ class ExpressionDependency extends Dependency */ protected function generateDependencyData() { - return $this->evaluateExpression($this->expression); + return eval("return {$this->expression};"); } } diff --git a/tests/unit/framework/base/ObjectTest.php b/tests/unit/framework/base/ObjectTest.php index feb2742..0557140 100644 --- a/tests/unit/framework/base/ObjectTest.php +++ b/tests/unit/framework/base/ObjectTest.php @@ -116,13 +116,6 @@ class ObjectTest extends \yiiunit\TestCase $this->assertTrue(isset($this->object->Text)); $this->assertTrue(empty($this->object->Text)); } - - public function testEvaluateExpression() - { - $object = new NewObject; - $this->assertEquals('Hello world',$object->evaluateExpression('"Hello $who"',array('who' => 'world'))); - $this->assertEquals('Hello world',$object->evaluateExpression(array($object,'exprEvaluator'),array('who' => 'world'))); - } }