• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make/use global variables in self made libraries

#1
[eluser]Unknown[/eluser]
Hi Everyone

Okay I've made my own library and have multiple functions in a given library. Now within each function they share a common variable. Example:

Code:
class myclass
{  
function A()
   {
      $myvar
   }

function B()
   {
      $myvar
   }
}

is there anyway this can be achieved but without using a session or config files??

#2
[eluser]apodner[/eluser]
would making it a class property do what you need it to do?


#3
[eluser]InsiteFX[/eluser]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by Php Designer 8.
* Date : 5/10/2012
* Time : 4:02:10 AM
* The Learn CodeIgniter Development Team.
* ------------------------------------------------------------------------
*
* Class Properties
*
* @package  Package  Application
* @subpackage Subpackage properties
* @category category properties
* @author  Raymond L King Sr.
* @copyright Copyright (c) 2009 - 2012, Custom Software Designers, LLC.
* @link  http://example.com
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/

/**
* SETUP:
*
* place this library in ./application/libraries/Property.php
*
* Autoload: ./application/libraries/Property.php
* Autoload: ./application/helpers/property_helper.php
*
* TESTING:
*
* In your Controller index method add.
*
*   property_set('1', 'test1', 'test1');
*   property_set('1', 'test2', 'test1');
*   property_set('1', 'test3', 'test1');
*
*   property_set('2', 'test4', 'test2');
*   property_set('2', 'test5', 'test2');
*   property_set('2', 'test6', 'test2');
*
*   $result  = property_get('1', 'test3');
*   $result1 = property_get_index('2');
*   $exist   = property_exists('2', 'test1');
*
*   $result2 = property_delete('1', 'test3');
*
*   // Remove the remarks // to reset and clear out the properties array.
*   // property_clear();
*
* DEBUGING:
*
*   var_dump($this->property->debug_properties());
*
*   $this->property->var_debug($this->property->debug_properties(), $result, $result1, $exist, $result2);
*
* USAGE:
*
* set:   $this->property->set('1', 'test1', 'test1');
* get:   $result = $this->property->get('1', 'test3');
* get_index $result = $this->property->get_index('2');
* exists:  $result = $this->property->exists('2', 'test1');
* clear:  $this->property->clear();
* delete:  $result2 = $this->property->delete('1', 'test3');
*
*/

class Property {

/**
  * -----------------------------------------------------------------------
  * Class variables - public, private, protected and static.
  * -----------------------------------------------------------------------
  */

/**
  * array ( $index => array ( $key => $val ));
  */
private static $properties = array(array());

// -----------------------------------------------------------------------

/**
  * __construct()
  *
  * Constructor PHP 5+ NOTE: Not needed if not setting values!
  *
  * @access public
  * @return void
  */
public function __construct()
{
  log_message('debug', 'Property Class Initialized');
}

// -----------------------------------------------------------------------

/**
  * set()
  *
  * Set a property index and key, value pair.
  *
  * @access public
  * @param string - $index - The property index
  * @param string - $key  - The property key
  * @param string - $val  - The property value
  * @return void
  */
public function set($index, $key, $val)
{
  if ( ! isset($this->properties[$index][$key]))
  {
   $this->properties[$index][$key] = $val;
  }
}

// -----------------------------------------------------------------------

/**
  * get()
  *
  * Gets a property and value.
  *
  * @access public
  * @param string - $index - The property index
  * @param string - $key  - The property key
  * @return mixed - Property string or boolean FALSE
  */
public function get($index, $key)
{
  if (isset($this->properties[$index][$key]))
  {
   return $this->properties[$index][$key];
  }

  return FALSE;
}

To be Continued below:

#4
[eluser]InsiteFX[/eluser]
Code:
// -----------------------------------------------------------------------

/**
  * get_index()
  *
  * Gets the array for this index.
  *
  * @access public
  * @param string - $index
  * @return mixed
  */
    public function get_index($index = NULL)
    {
     if ($index != NULL)
     {
      return $this->properties[$index];
     }

  return FALSE;
    }

// -----------------------------------------------------------------------

/**
  * exists()
  *
  * Checks to see if a property exists.
  *
  * @access public
  * @param string - $index - The property index
  * @param string - $key  - The property key
  * @return bool - TRUE if the property exists otherwise FALSE.
  */
public function exists($index, $key)
{
  return isset($this->properties[$index][$key]);
}

// -----------------------------------------------------------------------

/**
  * clear()
  *
  * Clears out and resets the property arrays.
  *
  * @access public
  * @return void
  */
public function clear()
{
  $this->properties = array(array());
}

// -----------------------------------------------------------------------

/**
  * delete()
  *
  * Deletes a property index key.
  *
  * @access public
  * @param string - $index - The property index
  * @param string - $key  - The property key
  * @return bool
  */
public function delete($index, $key)
{
  if (isset($this->properties[$index][$key]))
  {
   unset($this->properties[$index][$key]);
   return TRUE;
  }

  return FALSE;
}

// -----------------------------------------------------------------------

/**
  * debug_properties()
  *
  * Debug the properties arrays.
  *
  * @access public
  * @return array
  */
public function debug_properties()
{
  return $this->properties;
}

// -----------------------------------------------------------------------

/**
  * Debug Helper
  *
  * Outputs the given variable(s) with formatting and location.
  *
  * @access public
  * @param mixed - variables to be output.
  * @return void
  */
public function var_debug()
{
  list ($callee) = debug_backtrace();

  $arguments = func_get_args();

  $total_arguments = count($arguments);

  echo '<fieldset  #fefefe !important; border: 1px red solid; padding: 0px 0px 0px 20px">';
  echo '<legend  lightgrey; padding: 4px;">' . $callee['file'] . ' @ line: ' . $callee['line'] . '</legend><pre>';

  $i = 0;

  foreach ($arguments as $argument)
  {
   echo '<strong>Debug #' . (++$i) . ' of ' . $total_arguments . '</strong>: '."<br />";
   var_dump($argument);
   echo "\n";
  }

  echo "</pre>";
  echo "</fieldset><br />";

  exit ('Finished dumping debug variables.');
}

} // End of Class


/* -------------------------------------------------------------------------
* End of file Property.php
* Location: ./application/libraries/Property.php
* -------------------------------------------------------------------------
*/

#5
[eluser]InsiteFX[/eluser]
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by Php Designer 8.
* Date  : 5/20/2012
* Time  : 11:29:08 AM
* Author: Raymond L King Sr.
* The Learn CodeIgniter Development Team.
*
* Helper Name
*
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/

// ------------------------------------------------------------------------

/**
* property_set()
*
* Sets a new property.
*
* Usage: property_set($index, $key, $val);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_set'))
{
function property_set($index = '', $key = '', $val = '')
{
        $CI =& get_instance();
  $CI->property->set($index, $key, $val);
}
}

// ------------------------------------------------------------------------

/**
* property_get()
*
* Gets a property value.
*
* Usage: $result = property_get($index, $key);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_get'))
{
function property_get($index = '', $key = '')
{
        $CI =& get_instance();
  return $CI->property->get($index, $key);
}
}

// ------------------------------------------------------------------------

/**
* property_get_index()
*
* Get a property index.
*
* Usage: $result = property_get_index($index);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_get_index'))
{
function property_get_index($index = NULL)
{
        $CI =& get_instance();
  return $CI->property->get_index($index);
}
}

// ------------------------------------------------------------------------

/**
* property_exists()
*
* Checks to see if a property exists.
*
* Usage: $result = property_exists($index, $key);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_exists'))
{
function property_exists($index = '', $key = '')
{
        $CI =& get_instance();
  return $CI->property->exists($index, $key);
}
}

// ------------------------------------------------------------------------

/**
* property_delete()
*
* Deletes a property.
*
* Usage: $reult = property_delete($index, $key);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_delete'))
{
function property_delete($index = '', $key = '')
{
        $CI =& get_instance();
  return $CI->property->delete($index, $key);
}
}

// ------------------------------------------------------------------------

/**
* property_clear()
*
* Resets and clears out the properties array.
*
* Usage: property_clear();
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_clear'))
{
function property_clear()
{
        $CI =& get_instance();
  $CI->property->clear();
}
}


/* ------------------------------------------------------------------------
* End of file property_helper.php
* Location: ./application/helpers/property_helper.php
* ------------------------------------------------------------------------
*/

#6
[eluser]bigbusty[/eluser]
Have you tried making this variable static ?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.