Zend framework tutorial: Zend Registry


In php programming we always used global or $_GLOBALS to refer to the global variable.

For example consider below –

Db.php

  1. <?php
  2. // connect to mysql server
  3. $mysqlLink = mysql_connect(“mysql_server”, “mysql_user”, “mysql_password”);
  4. ?>
<?php

// connect to mysql server
$mysqlLink = mysql_connect("mysql_server", "mysql_user", "mysql_password");
?>

index.php

  1. <?php
  2. require_once “Db.php”;
  3. function displayTables()
  4. {
  5. global $mysqlLink;
  6. $sql = “SHOW TABLES”;
  7. //use global variable mysqlLink, and perform query execution
  8. mysql_query($sql, $mysqlLink);
  9. ?>
<?php
require_once "Db.php";
function displayTables()
{
global $mysqlLink;

$sql = "SHOW TABLES";

//use global variable mysqlLink, and perform query execution

mysql_query($sql, $mysqlLink);

?>

In zend framework, zend registry is an mechanism to using global storage. The registry is used as container for storing reusable objects and values, which will be available throughout our application.Zend framework examples | Zend registry -Example 1: In this example we will store the consumer name, in the consumer session name space. And register the consumer session namespace in the zend registry.

  1. <?php
  2. require_once “Zend/Session.php”;
  3. require_once “Zend/Session/Namespace.php”;
  4. Zend_Session::start();
  5. $consumer = new Zend_Session_Namespace(“consumer”);
  6. Zend_Registry::set(’consumer’, $consumer);
  7. $consumer->name = “Ganesh”;
  8. ?>
<?php
require_once "Zend/Session.php";
require_once "Zend/Session/Namespace.php";

Zend_Session::start();
$consumer = new Zend_Session_Namespace("consumer");
Zend_Registry::set(’consumer’, $consumer);
$consumer->name = "Ganesh";
?>

In this example, we will retrieve the session value, using zend registry.

  1. <?php
  2. echo “Consumer name:” . Zend_Registry::get(‘consumer’)->name;
  3. ?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s