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!
LVL 1
astralliquidAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
astralliquidAuthor Commented:
Thank you! You got me going with lots of new knowledge in OO and registry pattern..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.