EllisLab text mark
Advanced Search
     
using global variable or method within one class,
Posted: 09 September 2007 04:20 AM   [ Ignore ]
Avatar
Joined: 2007-08-30
12 posts

I have little knowledge of OOP of PHP!...
but CI is a good framework even though I’m not good at the OOP..

I’d like to use some variable globbally in the method of a class..

class Board extends Controller{
   
function Board(){
      parent
::Controller();
      
$this->load->model('board/board_admin_model''board_admin'TRUE);    
 ....
      if(!
$code$code="forum"//--> error this line..
      
$query $this->board_admin->getBoardConfig($code);
      
$BoardConfig $query->row();
    
}

   
function bRead($code,$no){

      
if($BoardConfig->name)//--> error this line
      
....
      
}
   }


even if I defined the $code, $BoardConfig in the consturctor,
I can’t use those within the bRead( , ) method

how do i do if I’d like to use global variable within other method in the same class??

 
Posted: 09 September 2007 08:59 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-04-22
56 posts
OneCorea - 09 September 2007 08:20 AM

even if I defined the $code, $BoardConfig in the consturctor, I can’t use those within the bRead( , ) method

how do i do if I’d like to use global variable within other method in the same class??

If you define the variables at the class level (i.e. within the class, but outside any function) you’ll be able to access them later using the $this keyword.  This should do what you’re after:

class Board extends Controller{
   
var $code;
   var 
$BoardConfig;

   function 
Board(){
      parent
::Controller();
      
$this->load->model('board/board_admin_model''board_admin'TRUE);    
 ....
      if(!
$this->code$code="forum"
      
$query $this->board_admin->getBoardConfig($code);
      
$this->BoardConfig $query->row();
    
}

   
function bRead($code,$no){

      
if($this->BoardConfig->name)
      
....
      
}
   }

If you’re using PHP 5, you can change the “var” in front of those variables to “public” or “private” and control the visibility of those variables.  “public” makes those variables available to other users of the class, while “private” limits their visibility to functions within the class.  The PHP manual has some more information on visibility which might be of assistance if you’re using PHP5.

Jim.

 
Posted: 09 September 2007 11:54 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2006-07-10
485 posts
Jim OHalloran - 10 September 2007 12:59 AM

If you’re using PHP 5, you can change the “var” in front of those variables to “public” or “private” and control the visibility of those variables.  “public” makes those variables available to other users of the class, while “private” limits their visibility to functions within the class.  The PHP manual has some more information on visibility which might be of assistance if you’re using PHP5.

Jim.

There is also ‘protected’ which restricts visibility to classes extended from a parent class. That is, the variable would have private visibility to any class that was not extended from a parent class.

In a language like C++, private and protected members are deemed read-only but this not the case with PHP5, but it’s supposed to be fixed in PHP6. However, there is a class on phpclasses.org that does make private and protected members read-only.