closure Object in array value

In Yii 1.x could do the following code block in the view




...

$configFields = array(

	'attribute' => array (		

		array(

			'name' => 'abbreviation',

			'listData' => function() {

							$states = State::find()->all();

							$listDataState = ArrayHelper::map($states, 'id_state', 'description');

								

							return $listDataState;

						},

			'type' => 'ActiveFieldDropDownList',

			),							

	)

);

...



But the same code in Yii 2.x returns something else:




closure Object

(

     [This] => yii \ web \ View Object

     ...



I wish I could create functions the way it did in 1.x Yii to a value of an array. How can I do this? Remember that this code is just an example and not the real situation. The function will be more complex than the above example …

Check the following. :)

http://www.yiiframework.com/doc-2.0/guide-intro-upgrade-from-v1.html#views

There too say:

So how would the solution to the above code?

Where do you define and how do you refer to your $configFields? If you define it in the controller and try to access it in the view by "$this->configFields", then it will not work.

Or, if you define it in the view script and use $this to refer to the controller, it also won’t work as expected.

There is no way to associate a value of an array in the view through a function?

Even if it is a function returning a string does not work … Example:




$configFields = array(

	'test' => function() {

		return "test"; // its dont work... ?

	}

);	




print "<pre>";

print_r( $configFields['test'] ); // return "Closure Object ..."

print "</pre>";



This happens both in the controller and in view.

I see.

Sorry, but I don’t have any idea at the moment.

Anonymous function dont work in Yii 2.x ?

Run this code in your 1.1 view script, and you’ll find it also returns “Closure Object …”.

Yes, it does. But only in the place where applicable. It has been the same with Yii 1.1. Your closure will do the job, for instance, for the value of a grid view column. But not always.

Where and how did you refer to the closure in Yii 1.1?

Where? Controller or View

How? Just as in the example above.

The example above works in 1.x Yii the controller and also the View.

You could write a small sample code as i do the key of an array receive an Anonymous function as in the previous example? Because I can not do this in Yii 2.x in the view or controller.

No.

It doesn’t display “test”, but “Closure Object ( [this] => SiteController Object …” in Yii 1.1 app.

I did write your small sample code into a view code (and a controller code) of a Yii 1.1 app of my own, and confirmed it didn’t display “test”. Did you test your small sample code (as it is) in Yii 1.1?

That’s not what I wanted to know.

CDataColumn::value, for example, can take a function.

http://www.yiiframework.com/doc/api/1.1/CDataColumn#c9734

So probably you could write




    'value' => $configFields['attribute']['listData']



Are you doing something like this in Yii 1.1?