gpt4 book ai didi

php - 如果对象通过别名传递,你什么时候通过引用传递?

转载 作者:可可西里 更新时间:2023-11-01 14:04:17 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
In PHP can someone explain cloning vs pointer reference?

根据 http://php.net/manual/en/language.oop5.references.php

One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

如果对象通过别名或处理程序传递,那么在什么情况下您实际上希望通过引用传递?

myFunc($obj); // Pass by alias/handler

myFunc(& $obj); // Pass by reference (when would you do this?)

myFunc(clone $obj); // Create a new object

每种情况有哪些不同的用例?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com