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.

Link to comment
Share on other sites

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' );

Link to comment
Share on other sites

the $mainframe variable in Joomla is it's core. you can use it to access core information. since they support PHP4, global is the only way to share that variable around. in this case, you have to use global if you want to use the mainframe.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.