php - How can I access a variable in a class which is imported by main page? -


i searching way access variable in function other function in same class. searched using global variable. works when created methods , printing codes on same page(and not class), when separate methods in class , call them main page, didn't work.

oh..and found can't use global variable because $rand_type should different every time i_type() method iterates in table, on main page. , need use same value of $rand_type in both methods.

(the situation is... in game, i'm going randomly print different types of items first, click 1 of them determine class , level randomly.)

how can solve it?

class item {      function i_type() {         $rand_type = rand(1,8);         // other codes below..         return $some_data;     }      function i_buy() {          $rand_class = rand(1,3);         $rand_level = rand(1,5);         // other codes below..         return $some_data;     } } 

you set private or public variables (private safer restricted access).

class item {     private $rand_class;     private $rand_level;     function getrandlevel() {         return $this->rand_level;     }     function setrandlevel($param) {         //clean variable before setting value if needed         $this->rand_level = $param;     } } 

then call function after making instance of class

$class = new item(); $rand_level = $class->getrandlevel(); $setlvl = 5; $class->setrandlevel($setlvl); 

this called encapsulation. higher concept. private/public variables access that.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -