在 PHP 中:如何在之前在另一个函数中定义的一个函数中调用 $variable?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2483675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:39:27  来源:igfitidea点击:

In PHP: How to call a $variable inside one function that was defined previously inside another function?

phpfunction

提问by Sam

I'm just starting with Object Oriented PHP and I have the following issue:

我刚开始使用面向对象的 PHP,我有以下问题:

I have a class that contains a function that contains a certain script. I need to call a variable located in that script within another function further down the same class.

我有一个类,其中包含一个包含某个脚本的函数。我需要在同一个类的另一个函数中调用位于该脚本中的变量。

For example:

例如:

class helloWorld {

function sayHello() {
     echo "Hello";
     $var = "World";
}

function sayWorld() {
     echo $var;
}


}

in the above example I want to call $var which is a variable that was defined inside a previous function. This doesn't work though, so how can I do this?

在上面的例子中,我想调用 $var ,它是一个在前一个函数中定义的变量。但这不起作用,那么我该怎么做呢?

回答by Marcx

you should create the var in the class, not in the function, because when the function end the variable will be unset (due to function termination)...

您应该在类中创建 var,而不是在函数中,因为当函数结束时,变量将被取消设置(由于函数终止)...

class helloWorld {

private $var;

function sayHello() {
     echo "Hello";
     $this->var = "World";
}

function sayWorld() {
     echo $this->var;
}


}
?>

If you declare the Variable as public, it's accessible directly by all the others classes, whereas if you declare the variable as private, it's accessible only in the same class..

如果将变量声明为public,则所有其他类都可以直接访问它,而如果将变量声明为private,则只能在同一类中访问它。

<?php
 Class First {
  private $a;
  public $b;

  public function create(){
    $this->a=1; //no problem
    $thia->b=2; //no problem
  }

  public function geta(){
    return $this->a;
  }
  private function getb(){
    return $this->b;
  }
 }

 Class Second{

  function test(){
    $a=new First; //create object $a that is a First Class.
    $a->create(); // call the public function create..
    echo $a->b; //ok in the class the var is public and it's accessible by everywhere
    echo $a->a; //problem in hte class the var is private
    echo $a->geta(); //ok the A value from class is get through the public function, the value $a in the class is not dicrectly accessible
    echo $a->getb(); //error the getb function is private and it's accessible only from inside the class
  }
}
?>

回答by Rob

Make $vara class variable:

创建$var一个类变量:

class HelloWorld {

    var $var;

    function sayHello() {
        echo "Hello";
        $this->var = "World";
    }

    function sayWorld() {
        echo $this->var;
    }

}

I would avoid making it a global, unless a lot of other code needs to access it; if it's just something that's to be used within the same class, then that's the perfect candidate for a class member.

我会避免将其设为全局,除非有很多其他代码需要访问它;如果它只是要在同一个类中使用的东西,那么它就是类成员的完美候选者。

If your sayHello()method was subsequently calling sayWorld(), then an alternative would be to pass the argument to that method.

如果您的sayHello()方法随后调用sayWorld(),则另一种方法是将参数传递给该方法。