简体   繁体   English

我可以在 PHP 中的不同函数之间共享变量吗?

[英]Can I share variables between different functions in PHP?

I'll try to explain with an example...我会试着用一个例子来解释......

Let's say I have two different functions, and one of them has a defined variable.假设我有两个不同的函数,其中一个有一个定义的变量。 In the second function, I don't wanna write the same variable again, can I simply use the variable from the first function in the second one WITHOUT redefining it in the second function?在第二个函数中,我不想再写同一个变量,我可以简单地在第二个函数中使用第一个函数中的变量而不在第二个函数中重新定义它吗?

Something like:就像是:

function a()
{
  $var = "my variable";
}

function b()
{
 echo $var;
}

Sorry if this questions is a bit silly, but I'm still a beginner =).对不起,如果这个问题有点傻,但我还是个初学者 =)。

The cleanest solution here is to make A and B methods of a class and have the common variables as private static variables. 这里最干净的解决方案是创建一个类的A和B方法,并将公共变量作为私有静态变量。 This removes the messiness of globals and allows both functions to be globally accessible. 这消除了全局变量的混乱,并允许两个函数全局访问。

class SomeClass
{
    private static $a = 1;

    public static function a() {
        self::$a = 2;
    }

    public static function b() {
        echo self::$a;
    }
}

You can then call the functions: 然后你可以调用这些函数:

SomeClass::a();
SomeClass::b();

Sure you can do globals, but of PHP 5.3.0+ has anonymous functions , so you can also do closures: 当然你可以做全局,但PHP 5.3.0+有匿名函数 ,所以你也可以做闭包:

<?php

function a(){
  $a = 1;  
  echo "First: $a, ";
  ++$a;

    // This is a closure. It can gain access to the variables of a() with the 
    // use option.
  $b = function() use ($a) {
      echo "second: $a";  
  };  
  $b();
};
a(); // Outputs: First: 1, second: 2
?>

Try it out at this Codepad example 在这个Codepad示例中尝试一下


or probably more useful: 或者可能更有用:

<?php

function a(){
  $a = 1;  
  echo "First: $a, ";
  ++$a;
  $b = function() use ($a) {
      echo "second: $a";  
  };        
  return $b;
};
$fun = a();     // $fun is now $b & $b has access to $a!
$fun();
// Outputs: First: 1, second: 2
?>

Try it out at this Codepad example 在这个Codepad示例中尝试一下

From the docs: 来自文档:

Closures may also inherit variables from the parent scope. 闭包还可以从父作用域继承变量。 Any such variables must be declared in the function header. 必须在函数头中声明任何此类变量。 Inheriting variables from the parent scope is not the same as using global variables. 从父作用域继承变量与使用全局变量不同。 Global variables exist in the global scope, which is the same no matter what function is executing. 全局变量存在于全局范围内,无论执行什么功能,它都是相同的。 The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from). 闭包的父作用域是声明闭包的函数(不一定是从中调用它的函数)。

$a = 1;
function a(){
  global $a;
  $a = 2;
  }
function b(){
  global $a;
  echo $a;
  }

a();
b();

output: 输出:

2

Like this . 像这样 。 you can use 您可以使用

function a()
{
  global $var; 
  $var = "my variable";
}

function b()
{
 global $var; 
 echo $var;
}

I'd do it like this. 我会这样做的。 This is a more common implementation. 这是一种更常见的实现。 Remember that functions can be passed parameters and can return values. 请记住,函数可以传递参数并可以返回值。

// Main
$a = a();
b($a);

function a()
{
  return "my variable";
}

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

You have the main body of a program, and the variable $a remains in scope there. 你有一个程序的主体,变量$a仍然在那里。 Globals are rarely used, and technically never needed. Globals很少使用,技术上从不需要。

<?php

class A
{
    private $p;
    private $q;
    private $ans;
    public  function hello()
    {
        echo "Hello PratikPatel";
    }
    public function getdata($a,$b)
    {

        $this->p=$a;
        $this->q=$b;
        $ans=$this ->p/$this->q;
        $this->ans=$ans;

    }
    public function div1()
    {
        echo "your division answer is:=".$this->ans."<br/>";
    }
}
class B extends A
{
    function hello()
    {
        echo "Hello "."<br/>";
    }

}

$p=new B();
$p1=new B();
$p2=new B();
$p -> getdata(20,10);
echo $p1 -> hello();
$p2 -> div1();

?> ?>

this code in problem that can not a print a division result 此代码中的问题无法打印除法结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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