【问题标题】:PHP: Access a variable that's declared in a function, within another functionPHP:访问在函数中声明的变量,在另一个函数中
【发布时间】:2020-09-23 10:40:03
【问题描述】:

假设我有以下内容:

class Submit extends \RightNow\Models\Base
{
    function __construct()
    {
        parent::__construct();
    }
    function inner () {
        return $i++;
    }
    function outer(){
        $i = 0;
        $this->inner();
        echo $i;
    }
}

$submit = new Submit();
$submit->outer();

inner() 如何访问在outer() 中声明的变量 $i?我试过使用global,但这没有用。

我理解在我非常基本的示例中,我可以将变量作为参数传递,但这只是作为我的问题的示例

我正在使用 CodeIgniter

【问题讨论】:

标签: php


【解决方案1】:

正如您提到的,您使用的是 Codeigniter,您可以通过以下代码实现它。在任何类中,您都可以在该类的任何成员函数中使用类成员变量。

class Submit extends \RightNow\Models\Base
 {
  public $i = 0;
  function __construct()
   {
      parent::__construct();
   }
  function inner () {
    return $this->i++;
   }
  function outer(){
    $this->i = 0;
    inner();
    echo $this->i;
   }
  outer();
}

【讨论】:

    【解决方案2】:

    如果您的所有方法都在一个中,您可以使用以下方法,其中变量是类中设置的类引用,因此自动可用于所有类方法。

    类中的变量称为属性

    class Submit extends \RightNow\Models\Base
    {
        private $i = 0; // set class based variable;
    
        function __construct()
        {
            parent::__construct();
        }
        function inner () {
            return $this->i++;
        }
        function outer(){
           // $i = 0; You probably don't want to set this each call? 
            $this->inner();
            echo $this->i; 
        }
    }
    
    $submit = new Submit();
    $submit->outer();
    

    上面的所有内容都引用了在类中设置的类属性$i

    或者,如果您在多个变量上使用相同的过程,则可以将它们分组到 array 中,然后简单地将数组作为单个参数传递。

    根据值是来自类内部还是外部,您的代码看起来会略有不同。

    参考资料:

    PHP - passing variables to classes

    https://www.php.net/manual/en/language.oop5.basic.php

    https://www.php.net/manual/en/language.oop5.properties.php

    【讨论】:

      【解决方案3】:

      您可以pass a variable by reference。例如:

      function inner (&$i) {
          return $i++;
      }
      
      function outer(){
          $i = 0;
          inner($i);
          echo $i; // 1
      }
      

      UPD

      如果要传递多个变量:

      1. 您可以通过引用传递数组:
      function inner (&$arr) {
         $arr['i']++;
         $arr['j']--;
      }
      
      function outer() {
         $a = array('i' => 5, 'j' => 10);
         inner($a);
         echo $a['i'] . "\n" . $a['j']; // 6 9
      }
      
      1. 您可以传递一个对象。在这种情况下,它总是通过引用隐式传递。例如
      function inner($obj) { // no & needed, because it is an object reference itself
          $obj->a++;
          $obj->b--;
      }
      
      
      function outer() {
          $obj = new \stdClass();
          $obj->a = 5;
          $obj->b = 10;
          inner($obj);
          echo $obj->a . "\n" . $obj->b;  // 6 9
      }
      
      1. 在同一个类中,您可以使用私有成员,但要确保函数不是递归的(即同一字段不会在不同的地方同时重用):
      class Submit extends \RightNow\Models\Base
      {
          private $_i;
      
          function __construct()
          {
              parent::__construct();
          }
          function inner () {
              $this->_i++;
          }
          function outer(){
              $this->_i = 0;
              $this->inner();
              echo $this->_i;
          }
      }
      

      【讨论】:

      • 所以如果我有十几个参数,我必须这样做十几次?只有这样?
      猜你喜欢
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多