PHP中有指针吗? [英] Are there pointers in php?

查看:154
本文介绍了PHP中有指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码是什么意思?这就是您在php中声明指针的方式吗?

What does this code mean? Is this how you declare a pointer in php?

$this->entryId = $entryId;

推荐答案

PHP中的变量名称以$开头,因此$ entryId是变量的名称. $ this是PHP中面向对象编程中的一个特殊变量,它引用当前对象. ->用于访问PHP中的对象成员(如属性或方法),如C ++中的语法. 所以您的代码意味着:

Variable names in PHP start with $ so $entryId is the name of a variable. $this is a special variable in Object Oriented programming in PHP, which is reference to current object. -> is used to access an object member (like properties or methods) in PHP, like the syntax in C++. so your code means this:

将变量$ entryId的值放入此对象的entryId字段(或属性)中.

Place the value of variable $entryId into the entryId field (or property) of this object.

& PHP中的运算符,表示传递引用.这是一个示例:

The & operator in PHP, means pass reference. Here is a example:

$b=2;
$a=$b;
$a=3;
print $a;
print $b;
// output is 32

$b=2;
$a=&$b; // note the & operator
$a=3;
print $a;
print $b;
// output is 33

在上面的代码中,因为我们使用了&运算符,将$ b指向的引用存储在$ a中.因此,$ a实际上是对$ b的引用.

In the above code, because we used & operator, a reference to where $b is pointing is stored in $a. So $a is actually a reference to $b.

在PHP中,默认情况下按值传递参数(受C启发).因此,在调用函数时,当您传入值时,它们是按值复制的,而不是按引用复制的.这是默认的大多数情况".但是,在定义函数时,有一种方法可以通过引用行为进行传递.示例:

In PHP, arguments are passed by value by default (inspired by C). So when calling a function, when you pass in your values, they are copied by value not by reference. This is the default IN MOST SITUATIONS. However there is a way to have pass by reference behaviour, when defining a function. Example:

function plus_by_reference( &$param ) {
      // what ever you do, will affect the actual parameter outside the function
      $param++;
}

$a=2;
plus_by_reference( $a );
echo $a;
// output is 3

有许多内置函数的行为如下.就像对数组进行排序的sort()函数一样,该函数将直接影响该数组,并且不会返回另一个已排序的数组.

There are many built-in functions that behave like this. Like the sort() function that sorts an array will affect directly on the array and will not return another sorted array.

尽管有一些有趣的事情要注意.因为按值传递模式可能会导致更多的内存使用,并且PHP是一种解释性语言(因此,用PHP编写的程序不如已编译的程序快),所以要使代码运行更快并最大程度地减少内存使用量,需要进行一些调整.在PHP解释器中.一种是惰性复制(我不确定名称).这意味着:

There is something interesting to note though. Because pass-by-value mode could result in more memory usage, and PHP is an interpreted language (so programs written in PHP are not as fast as compiled programs), to make the code run faster and minimize memory usage, there are some tweaks in the PHP interpreter. One is lazy-copy (I'm not sure about the name). Which means this:

当您将一个变量对应到另一个变量时,PHP会将对第一个变量的引用复制到第二个变量中.因此,到目前为止,您的新变量实际上是对第一个变量的引用.该值尚未复制.但是,如果您尝试更改这些变量中的任何一个,PHP将复制该值,然后更改该变量.这样,如果您不更改值,则将有机会节省内存和时间.

When you are coping a variable into another, PHP will copy a reference to the first variable into the second variable. So your new variable, is actually a reference to the first one until now. The value is not copied yet. But if you try to change any of these variables, PHP will make a copy of the value, and then changes the variable. This way you will have the opportunity to save memory and time, IF YOU DO NOT CHANGE THE VALUE.

所以:

$b=3;
$a=$b;
// $a points to $b, equals to $a=&$b
$b=4;
// now PHP will copy 3 into $a, and places 4 into $b

所有这些之后,如果要将$ entryId的值放入对象的'entryId'属性中,则上述代码将执行此操作,并且不会复制entryId的值,直到您更改其中的任何一个为止,结果减少内存使用量.如果您实际上希望它们都指向相同的值,请使用以下方法:

After all this, if you want to place the value of $entryId into 'entryId' property of your object, the above code will do this, and will not copy the value of entryId, until you change any of them, results in less memory usage. If you actually want them both to point to the same value, then use this:

$this->entryId=&$entryId;

这篇关于PHP中有指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆