开发者

how can i include php file in php class

i开发者_JS百科 have php with an array i.e

$var = array(
   "var" => "var value",
   "var2" => "var value1"
);

and have another file with a class i.e

class class1{
   function fnc1(){
        echo $var['var2'];
       //rest of function here
   }
}

now how can i get $var['var'] in class file in function fnc1()


You can pass it as an argument, or use the global keyword to put it in the current scope. However, using global is discouraged, try passing it as an argument.


Pass it as an argument?

class class1{
  function fnc1($var) {
    echo $var['var2'];
  }
}

And in your other file call this class method with your array as an argument.


From: http://php.net/manual/en/function.include.php

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

So you could do

class class1
{
   function fnc1()
   {
        include 'thefile.php'
        echo $var['var2'];
       //rest of function here
   }
}

but like others pointed out before, you dont want to do that, because it introduces a dependency on the filesystem in your class. If your method requires those variables to work, then inject them as method arguments or pass them into the constructor and make them a property (if you need them more often). This is called Dependency Injection and it will make your code much more maintainable in the long run, e.g. do

class class1
{
    private $data;
    public function __construct(array $var)
    {
        $this->data = $var;
    }
    function fnc1()
    {
        echo $this->data['var2'];
       //rest of function here
   }
}

and then do

$obj = new class1($var);
echo $obj->fnc1();

or require the data to be passed into the method on invocation

class class1
{
    function fnc1(array $var)
    {
        echo $var['var2'];
       //rest of function here
   }
}

and then

$obj = new class1;
$obj->fnc1($var);


You might use global $var in your included file, but it's really a bad practice, as another script, before your included file, might redefine the value/type of $var.

Example :

class class1{
   function fnc1(){
      global $var;
      echo $var['var2'];
      //rest of function here
   }
}

A better solution, is to pass your $var as a parameter to your fnc1(), even to your class1::__construct()


@Vindia: I'd prefer the argumant-style too, but would recommend either using type hint or a simple check to avoid warnings when accessing *non_array*['var2']:

// acccepts array only. Errors be handled outside
function fnc1(Array $var) {
    echo $var['var2'];
}


// accepts any type:
function fnc1(Array $var) {
    if (is_array($var)) {
        echo $var['var2'];
    }
}


class class1{
  function fnc1(){
    include 'otherFile.php';
    echo $var['var2'];
    //rest of function here
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜