Welcome Guest, Not a member yet? Register   Sign In
using global variable or method within one class,
#1

[eluser]OneCorea[/eluser]
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..

Code:
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??
#2

[eluser]Jim OHalloran[/eluser]
[quote author="OneCorea" date="1189340405"]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??[/quote]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:
Code:
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.
#3

[eluser]esra[/eluser]
[quote author="Jim OHalloran" date="1189400360"]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.[/quote]

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.




Theme © iAndrew 2016 - Forum software by © MyBB