PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Assigning Variables

Your controller script should assign necessary variables to the view before it hands over control to the view script. Normally, you can do assignments one at a time by assigning to property names of the view instance:

<?php
$view 
= new Zend_View();
$view->"Hay";
$view->"Bee";
$view->"Sea";

However, this can be tedious when you have already collected the values to be assigned into an array or object.

The assign() method lets you assign from an array or object "in bulk". The following examples have the same effect as the above one-by-one property assignments.

<?php
$view 
= new Zend_View();

// assign an array of key-value pairs, where the
// key is the variable name, and the value is
// the assigned value.
$array = array(
    
'a' => "Hay",
    
'b' => "Bee",
    
'c' => "Sea",
);
$view->assign($array);

// do the same with an object's public properties;
// note how we cast it to an array when assigning.
$obj = new StdClass;
$obj->"Hay";
$obj->"Bee";
$obj->"Sea";
$view->assign((array) $obj);

Alternatively, you can use the assign method to assign one-by-one by passing a string variable name, and then the variable value.

<?php
$view 
= new Zend_View();
$view->assign('a'"Hay");
$view->assign('b'"Bee");
$view->assign('c'"Sea");

Zend Framework