1. 云栖社区>
  2. PHP教程>
  3. 正文

PHP - Callbacks And Sharing Resources

作者:用户 来源:互联网 时间:2017-12-01 13:54:03

PHP - Callbacks And Sharing Resources - 摘要: 本文讲的是PHP - Callbacks And Sharing Resources, Something I keep questioning myself on is whether use()passes instantiated classes by reference or creates a copy, a

Something I keep questioning myself on is whether use()passes instantiated classes by reference or creates a copy, and whether this applies to the stdClassas well.

The script below demonstrates that all objects (including stdClass) are passed by reference but basic types such as integers are passed a copy. This is especially useful to know when playing with callbacks where we need to retrieve values but can't utilize return for whatever reason.

<?php$sharedResource = new stdClass();$sharedResource->value = 1;$integer = 1;$callback2 = function() use($sharedResource, $integer){print "value: $sharedResource->value" . PHP_EOL;print "Integer value: $integer" . PHP_EOL;};$callback = function() use($sharedResource, $integer){$sharedResource->value = 3;$integer = 3;};$callback();$callback2();# Output:# value: 3# Integer value: 1

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。