标签:php
我问这个var赋值定义可能会导致什么:
$component = $id = $class = '';
我的意思是IDE(phpStorm)允许,该代码不返回任何警告,致命信息或其他任何信息,但是….那里有什么?
你怎么看?
解决方法:
没关系
PHP在写系统上使用副本.这意味着在更改时确实会在内存中创建诸如字符串或整数之类的原始类型,并且在您的示例中可能还可以.
PHP
$component = $id = $class = 'foobar';
var_dump($component);
var_dump($id);
var_dump($class);
产量
string 'foobar' (length=6)
string 'foobar' (length=6)
string 'foobar' (length=6)
但是,当您对对象执行此操作时要小心,对象通过引用传递并且具有不同的行为:
PHP
class Obj
{
public $_name;
}
$a = $b = new Obj();
$component = $id = $class = 'foobar';
$b->_name = 'Reynier'; //try to change name of second object only
$id = 'Reynier'; // try to change $id only
var_dump($a);
var_dump($b);
var_dump($component);
var_dump($id);
var_dump($class);
产量
#Both object are changed
object(Obj)[1]
public '_name' => string 'Reynier' (length=7)
object(Obj)[1]
public '_name' => string 'Reynier' (length=7)
#only $id was changed
string 'foobar' (length=6)
string 'Reynier' (length=6)
string 'foobar' (length=6)
标签:php
来源: https://codeday.me/bug/20191009/1881338.html