CodeIgniter Forums
Global functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Global functions (/showthread.php?tid=33213)

Pages: 1 2


Global functions - El Forum - 08-19-2010

[eluser]mabright[/eluser]
OK. I tried this with error below.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: CI

Filename: libraries/Location.php

Line Number: 31
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/Location.php

Line Number: 31

Fatal error: Call to a member function userdata() on a non-object in

Code:

Code:
class Location {
    
    var $user_location;
    
    public function __construct()
    {
        $CI =& get_instance();
        $this->user_location = $this->get_user_location();
        ...
    }
    
    function get_user_location()
    {
        if($CI->session->userdata('location') == TRUE)
        {
            return $CI->session->userdata('location');
        }
        else
        {
                 ....



Global functions - El Forum - 08-19-2010

[eluser]mddd[/eluser]
If you use $CI in one method, it is not set in another method! You need to make a class property $CI and, in your methods, refer to it as $this->CI.

Code:
class Location
{
   var $CI;

  function __construct()
  {
    $this->CI =& get_instance();
  }

  function other_function()
  {
    // use $this->CI here
  }
}



Global functions - El Forum - 08-19-2010

[eluser]mabright[/eluser]
Wow...how much I still do not know...That did the trick, thanks alot...


Global functions - El Forum - 08-30-2010

[eluser]mabright[/eluser]
I have this global variable working in my view but when I reference $data['variable_name'] in my controller or model, I get an undefined variable error.


Global functions - El Forum - 08-30-2010

[eluser]cahva[/eluser]
If you're sending $data to view:
Code:
$data['variable_name'] = 'Foo';

$this->load->view('layout',$data);

..then you would reference it as $variable_name in the view.

EDIT: misread sorry. You ofcourse have to send the $data to your lib or model.
BTW, If you need some information in almost every page, you should concider MY_Controller technique where you extend from that. Or even better, take advantage of autoloading classes in PHP5 and use technique described here by Phil Sturgeon.