• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

how to declare global scope?

I just started learning on oo code and created my own simple framework but I found declaring global for everything in the objects is very messy and tedious. How do I declare all objects in one place and accessible to every other objects?

Right now I am doing something like this..

<?php
  $config = new ConfigObj()
  $db = new DatabaseObj()
  $app = new MainApp()
  $app->someFunction()
?>

class MainApp {
  global $db, $config
...
}

class Obj2 {
  global $db, $config, Obj1
...
}

class Obj2 {
  global $db, $config, Obj1
  $db->method();
  $Obj1->method();  
...
}

class SomeUtil {
  global $db, $config, Obj1, Obj2
}


As you can see, my global and objects are everywhere in order to see each other.. how to pros do it? How do I write a piece of code that will register all the objs to be accessible anywhere?

Thanks!
0
astralliquid
Asked:
astralliquid
  • 2
  • 2
1 Solution
 
SwafnilCommented:
Especially if you are using OOP, it's an absolute no-go to rely on global variables; this is just like guessing if an external variable really has been set. I would pass the necessary objects so they can be used inside your classes like in the below example; another way would be to have a central registry, i.e. like the one used inside Zend Framework:

http://framework.zend.com/manual/en/zend.registry.using.html
<?php

Class DBWrapper (){
  private $connection = false;

  public function open (){
    $this->connection = mysql_connect (...);
  } 

  public function query ($sql){
    ...
  }
}

Class UserList ()
{
  private $users = array();
  private $dbObj = false;

  public pass_dbwrapper (&$obj){
    $this->dbObj = $obj;
  }

  public load_users (){
    $users = $this->dbObj->query("SELECT * FROM Userlist");
  }
}

Open in new window

0
 
astralliquidAuthor Commented:
But then it will be like passing in many objects in some cases..

$obj = new Obj1($Obj2,$Obj3,$Obj4,$Obj5,$Obj6,$Obj7,$Obj8)    ??

Not sure how Zend registry works...is there anyway to simulate or code it like adding to registry?
0
 
SwafnilCommented:
You could write your own registry component, i.e. like in the below class, then you would only have to pass this class to your other class instances to allow accessing its variables. I hope the given example is not too complex.

I would also advice to take a look into the OOP section of PHP.net which holds a lot of useful information about the topic: http://de3.php.net/manual/en/language.oop5.php
<?php
error_reporting("E_ALL");
ini_set("display_errors", "1");

class Registry
{
  private $variables = array();
  
  public function set ($varName, $varValue)
  {
    $this->variables[$varName] = $varValue;
  }

  public function get ($varName)
  {
    if (!isset($this->variables[$varName])){
      return false;
    } else {
     return $this->variables[$varName];
    }
  }

  public function check ($varName)
  {
    if (isset($this->variables[$varName])){
      return true;
    } else {
      return false;
    }
  }
}


class DBConnect 
{
  private $registry = false;
  private $conn = false;
  private $errorMessage = "";
  private $errorCode = "";
  private $state = "UNINITIALIZED";
  
  public function set_registry ($reg){
    // please note that this doesn't copy the registry, instead it is still a reference to the original registry 
	// and all later changes will have an impact on your private variable $this->registry; 
	// if you ever want to copy a class instance, use clone() [http://de3.php.net/clone]
    $this->registry = $reg;
  }
  
  public function state ()
  {
    return $this->state;
  }
  
  public function connect (){
    if ($this->registry->check("dbhost")){
      $conn = mysql_connect ($this->registry->get("dbhost"), $this->registry->get("dbuser"), $this->registry->get("dbpass"));
	  if ($conn){
	    $this->state = "OPEN";
	  } else {
	    $this->state = "ERROR";
		$this->errorMessage = mysql_error();
		$this->errorCode = mysql_errno();
	  }	
	} else {
	  $this->state = "CONFIG MISSING";
    }	
  }	
}

class Config 
{
  private $registry = false;
  
  public function set_registry ($reg){
	$this->registry = $reg;
  }
  
  public function configure ($key, $value)
  {
	$this->registry->set ($key, $value);
  }
}

class MainApp 
{
  public function start (){
	  $registry = new Registry();
	  $registry->set("appname", "My test app");

	  
	  $config = new Config();
	  $config->set_registry($registry);
	  $config->configure ("dbhost", "localhost");
	  $config->configure ("dbname", "mysql");
	  $config->configure ("dbuser", "root");
	  $config->configure ("dbpass", "XXX");
	  
	  $connection = new DBConnect();
	  $connection->set_registry($registry);
	  $connection->connect();
	  print $connection->state();
	  
  }
}

$mainApp = new MainApp();
$mainApp->start();

Open in new window

0
 
astralliquidAuthor Commented:
Thank you! You got me going with lots of new knowledge in OO and registry pattern..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now