Php 如何将外部$var传递到函数()中

Php 如何将外部$var传递到函数()中,php,function,oop,Php,Function,Oop,现在,看看我的page.php,我将在其中解释我的问题 <?php $var= "<p>Hello world</p>"; function myfunction1($a){ echo $a; } //then myfunction1($var);//-->OK, return "Hello world!" //but, the thing is, i don't want to pass any argument into myfunction(),

现在,看看我的
page.php
,我将在其中解释我的问题

<?php
$var= "<p>Hello world</p>";

function myfunction1($a){
echo $a;
}

//then
myfunction1($var);//-->OK, return "Hello world!"


//but, the thing is, i don't want to pass any argument into myfunction(), 
//so i have to import the external $var into myfuntion2()


function myfunction2(nothing here){
//what's here?
}

myfunction2();//i want to do this

?>

当然,如果我将所有这些都封装在一个类中,那么
myfunction()
$var
将变成
method$amp;属性
(OOP样式), 这些将是如此容易访问!但我不想那样做


那么,有可能吗?谁能给我一个建议吗?如果您不想在函数中传递任何参数,请多谢,唯一的方法是使用
global

function myfunction2(){
     global $var;
}
但是我应该警告你,使用
全局
是非常糟糕的,所以除非你知道它的行为,否则不要使用它。您的
global$var
可以在函数中更改,例如

$var = 2; //Initial value
function myfunction2(){
     global $var;
     $var = 'changed';
}
myfunction2(); //$var is now holding 'changed'. 2 is now lost
因此,从现在起,
$var
将保留字符串
changed
,因为
$var
有一个
全局范围,它不再是函数的局部范围


或者,如果您不想在函数中传递任何参数,您可以阅读答案以将函数作为函数参数传递,唯一的方法是使用
全局

function myfunction2(){
     global $var;
}
但是我应该警告你,使用
全局
是非常糟糕的,所以除非你知道它的行为,否则不要使用它。您的
global$var
可以在函数中更改,例如

$var = 2; //Initial value
function myfunction2(){
     global $var;
     $var = 'changed';
}
myfunction2(); //$var is now holding 'changed'. 2 is now lost
因此,从现在起,
$var
将保留字符串
changed
,因为
$var
有一个
全局范围,它不再是函数的局部范围


或者,您可以阅读答案以将函数作为函数参数传递

您可以在现代版本的PHP中执行此操作:

$var = 'World';
$func = function() use($var){
  echo "Hello $var";
};

$func(); //=> Hello World

您可以在现代版本的PHP中执行此操作:

$var = 'World';
$func = function() use($var){
  echo "Hello $var";
};

$func(); //=> Hello World
或者使用单例:

function myfunction2(){
    echo MySingleton::getInstance()->var2;
}
或者使用单例:

function myfunction2(){
    echo MySingleton::getInstance()->var2;
}

必须在函数中将变量声明为全局变量,然后才能在函数中使用变量,而无需将其作为参数传递

像这样,

<?php
   $var= "<p>Hello world</p>";

   function myfunction1($a){
     echo $a;
   }
  //then
  myfunction1($var);//-->OK, return "Hello world!"

  //but, the thing is, i don't want to pass any argument into myfunction(), so i  have to    import the external $var into myfuntion2()
  function myfunction2(){
     global $var;
     echo $var;
  }

  myfunction2();//i want to do this

?>

必须在函数中将变量声明为全局变量,然后才能在函数中使用变量,而无需将其作为参数传递

像这样,

<?php
   $var= "<p>Hello world</p>";

   function myfunction1($a){
     echo $a;
   }
  //then
  myfunction1($var);//-->OK, return "Hello world!"

  //but, the thing is, i don't want to pass any argument into myfunction(), so i  have to    import the external $var into myfuntion2()
  function myfunction2(){
     global $var;
     echo $var;
  }

  myfunction2();//i want to do this

?>

Add
global$var内部
myfunction2()
。添加
global$var内部
myfunction2()
。我怀疑OP是否知道I singleton是什么。我在上课前教过全局变量的CS书籍。谢谢,我会考虑使用Singleton,稍后再测试!,无论如何,谢谢!我怀疑OP知道我单身是什么。我在上课前教过全局变量的CS书籍。谢谢,我会考虑使用Singleton,稍后再测试!,无论如何,谢谢!