简体   繁体   中英

PHP include outside class only Once

I'm a new guy in PHP OOP concepts. One of the first things that caught my eye that I can't include a php script to multiple classes just by writing it once at the begining of the script. I mean

<?php
include 'var.php';
class userSession{
  /* all the code */
  public function getVariables(){
   /* use of variables which are inside var.php */
  }
  public function getOtherVariables(){
   /* use of variables which are inside var.php */
  }
}
?>

this doesn't work.

I have to do this -

 <?php
    class userSession{
      /* all the code */
      public function getVariables(){
       include 'var.php';
       /* use of variables which are inside var.php */
      }
      public function getOtherVariables(){
       include 'var.php';
       /* use of variables which are inside var.php */
      }
    }
    ?>

Anything I'm Missing??

If the variables are defined in the global space, then you need to reference them in the global space within your class methods:

include 'var.php'; 
class userSession{ 
  /* all the code */ 
  public function getVariables(){ 
   global $var1, $var2;
   echo $var1,' ',$var2,'<br />';
   $var1 = 'Goodbye'
  } 
  public function getOtherVariables(){ 
   global $var1, $var2;
   echo $var1,' ',$var2,'<br />';
  } 
} 

$test = new userSession();
$test->getVariables();
$test->getOtherVariables();

This is not a good idea. Use of global variables is generally bad practise, and an indication that you don't really understand the principles of OOP yet.

In your second example, you're defining the variables in the local space for the individual methods

class userSession{ 
  /* all the code */ 
  public function getVariables(){ 
   include 'var.php'; 
   echo $var1,' ',$var2,'<br />';
   $var1 = 'Goodbye'
  } 
  public function getOtherVariables(){ 
   include 'var.php'; 
   echo $var1,' ',$var2,'<br />';
  } 
} 

$test = new userSession();
$test->getVariables();
$test->getOtherVariables();

Because each variable is defined independently within local method space, changing $var1 in getVariables() has no affect on $var1 in getOtherVariables()

A third alternative is to define your variables as class properties:

class userSession{ 
   include 'var.php'; 
  /* all the code */ 
  public function getVariables(){ 
   echo $this->var1,' ',$this->var2,'<br />';
   $this->var1 = 'Goodbye'
  } 
  public function getOtherVariables(){ 
   echo $this->var1,' ',$this->var2,'<br />';
  } 
} 

$test = new userSession();
$test->getVariables();
$test->getOtherVariables();

This defines the variables as properties in the userClass space, so they can be accessed by all methods in the instance of userClass. Note the use of $this->var1 rather than $var1 to access the properties. If you have multiple instances of userClass, the properties in each instance can be different, but within each instance, the properties are consistent across all methods of that instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM