无法访问 PHP 7 中闭包对象内部的属性。

huangapple go评论64阅读模式
英文:

Cannot access the properties inside the Closure object in php 7

问题

如何访问 cisBillDetailsObjectBase

我尝试过:

$cisBillDetailsObjectBase = $data->static['cisBillDetailsObjectBase'];
$data->static();
$data->static;
$data->{"
$cisBillDetailsObjectBase = $data->static['cisBillDetailsObjectBase'];
$data->static();
$data->static;
$data->{"\0Closure\0static"};
Closure
$cisBillDetailsObjectBase = $data->static['cisBillDetailsObjectBase'];
$data->static();
$data->static;
$data->{"\0Closure\0static"};
static"};
英文:

I have this in my unit test code:

$data = $this->_createSfApiFindMock($cisDataCount);
var_dump($data()); die();

output:

class Closure#731 (3) {
  public $static =>
  array(4) {
    'cisDataCount' =>
    int(201)
    'cisBillObjectBase' =>
    class stdClass#716 (20) {
      public $unisrv_dcis__TargetMonth__c =>
      string(6) "202301"
      .........
      public $unisrv_dcis__Member__r =>
      class stdClass#717 (1) {
        ...
      }
      public $unisrv_dcis__Contract__r =>
      class stdClass#718 (3) {
        ...
      }
    }
    'cisBillDetailsObjectBase' =>
    array(5) {
      [0] =>
      class stdClass#720 (11) {
        ...
      }
      [1] =>
      class stdClass#722 (11) {
        ...
      }
      [2] =>
      class stdClass#724 (11) {
        ...
      }
      [3] =>
      class stdClass#726 (11) {
        ...
      }
      [4] =>
      class stdClass#728 (13) {
        ...
      }
    }
    'maxDataChunkSize' =>
    int(200)
  }
}

How to get access to cisBillDetailsObjectBase?

I tried:

$cisBillDetailsObjectBase = $data->static['cisBillDetailsObjectBase'];
$data->static();
$data->static;
$data->{"
$cisBillDetailsObjectBase = $data->static['cisBillDetailsObjectBase'];
$data->static();
$data->static;
$data->{"\0Closure\0static"};
Closure
$cisBillDetailsObjectBase = $data->static['cisBillDetailsObjectBase'];
$data->static();
$data->static;
$data->{"\0Closure\0static"};
static"};

答案1

得分: 1

不能访问闭包的内容。闭包是一个可执行的函数。

看看这个简单的代码:

var_dump(function() { static $a; });

输出:

object(Closure)#1 (1) {
  ["static"]=>
  array(1) {
    ["a"]=>
    NULL
  }
}

由于作用域的原因,您无法访问该函数的内部数据。

您应该执行闭包以返回某些内容或与其他内容进行交互,以“导出”其数据。

示例:

$a = function() { static $a = 2; return $a; };
var_dump($a()); // 调用

输出:

int(2)
英文:

You cannot access to the content of a Closure. A Closure is a function to execute.

Take a look to this simple code:

var_dump(function() { static $a; });

Output

object(Closure)#1 (1) {
  ["static"]=>
  array(1) {
    ["a"]=>
    NULL
  }
}

You cannot access to the internal data of the function because of the scope.

You should execute the closure that return something or interact with something else to "export" its data.

Example:

$a = function() { static $a = 2; return $a; };
var_dump($a()); // CALL

Output:

int(2)

huangapple
  • 本文由 发表于 2023年2月24日 11:42:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定