Jump to content

[SOLVED] global question


dennismonsewicz

Recommended Posts

ah gotcha... what ways would those be?

 

I setup my DB Class with functions, so it can be called from anywhere in my script without adding a ton of overhead. In these functions I make the instantiated db class global so I do not have to have that object to pass in each time I call it.

 

That is a good way to use it, more for versatility. If you just have a regular variable say you want to add x + y, I would just pass both those in as parameters, as that is what the function is designed to do.

 

So it all depends on the circumstance.

cool thanks! I am trying to practice more OOP... still kind of new to OOP... not to sure i have a full grasp of it

 

If you want more in depth explanation post the code where you are using global at.

 

Well I am working within Joomla (CMS) to build a custom component and within its backend they have a pre-built function that will do pagination for you and they set a global.. so I am using the code but not necessarily understanding why its being used...

 

global $mainframe;
$context = 'com_component.' . $d . '.list.';

$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit',		'limit',		20, 'int' );
$limitstart	= $mainframe->getUserStateFromRequest( $context.'limitstart',	'limitstart',	0, 'int' );

for that DB case, i usually use the Singleton Design Pattern

 

I will have to read up on that.

 

cool thanks! I am trying to practice more OOP... still kind of new to OOP... not to sure i have a full grasp of it

 

If you want more in depth explanation post the code where you are using global at.

 

Well I am working within Joomla (CMS) to build a custom component and within its backend they have a pre-built function that will do pagination for you and they set a global.. so I am using the code but not necessarily understanding why its being used...

 

global $mainframe;
$context = 'com_component.' . $d . '.list.';

$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit',		'limit',		20, 'int' );
$limitstart	= $mainframe->getUserStateFromRequest( $context.'limitstart',	'limitstart',	0, 'int' );

 

Just remember, because one script does it that way does not mean it is the correct way. Especially a script like Joomla where their ultimate goal is being able to use the script on any version of php etc. Not really a good script to learn OOP from imo.

I will have to read up on that.

 

in a nutshell:

<?php
  class db {
    private static $instance;
    private $cnx;
    
    static function getInstance(){
      return self::$instance;
    }
    
    public function __construct ( $host, $user, $pass, $db ) {
      //Check for singleton
      if(self::$instance)
        throw new Exception("An instance already exists");
      //Connect
      $this->cnx = mysql_connect($host,$user,$pass);
      mysql_select_db($db,$this->cnx);
      //Add singleton
      self::$instance = $this;
    }
    public function query ( $sql ) {
      
    }
  }
  
  //Connect
  $db1 = new db('host','user','pass','db');
  //you can now use the variable $db1 as normal
  $db1->query();
  
  //or get it via the static method
  $db2 = db::getInstance();
  
  //or in a function
  function do_query ( ) {
    $db = db::getInstance();
    
  }
?>

 

be careful though, people tend to go crazy with singletons and use more memory then they need to

I will have to read up on that.

 

in a nutshell:

<?php
  class db {
    private static $instance;
    private $cnx;
    
    static function getInstance(){
      return self::$instance;
    }
    
    public function __construct ( $host, $user, $pass, $db ) {
      //Check for singleton
      if(self::$instance)
        throw new Exception("An instance already exists");
      //Connect
      $this->cnx = mysql_connect($host,$user,$pass);
      mysql_select_db($db,$this->cnx);
      //Add singleton
      self::$instance = $this;
    }
    public function query ( $sql ) {
      
    }
  }
  
  //Connect
  $db1 = new db('host','user','pass','db');
  //you can now use the variable $db1 as normal
  $db1->query();
  
  //or get it via the static method
  $db2 = db::getInstance();
  
  //or in a function
  function do_query ( ) {
    $db = db::getInstance();
    
  }
?>

 

be careful though, people tend to go crazy with singletons and use more memory then they need to

 

Yea that is exactly what I would need. Be a bit easier than doing that global stuff. Thanks for the example. And yea I only see myself using singleton for the db connection. Thanks rhodesa.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.