php php中有指针吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/746224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:44:05  来源:igfitidea点击:

Are there pointers in php?

phppointers

提问by farzad

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

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

$this->entryId = $entryId;

回答by farzad

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:

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

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

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

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

PHP 中的 & 运算符,表示传递引用。下面是一个例子:

$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

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.

在上面的代码中,因为我们使用了 & 运算符,所以 $b 指向的位置的引用存储在 $a 中。所以 $a 实际上是对 $b 的引用。

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:

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

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

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.

有许多内置函数的行为与此类似。就像对数组排序的 sort() 函数一样,将直接影响数组并且不会返回另一个已排序的数组。

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 编写的程序不如编译程序快),为了使代码运行得更快并最大限度地减少内存使用,有一些调整在 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.

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

So:

所以:

$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

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:

毕竟,如果您想将 $entryId 的值放入对象的 'entryId' 属性中,上面的代码会这样做,并且不会复制 entryId 的值,直到您更改其中任何一个,导致内存减少用法。如果您确实希望它们都指向相同的值,请使用以下命令:

$this->entryId=&$entryId;

回答by aliqandil

No, As others said, "There is no Pointer in PHP." and I add, there is nothing RAM_related in PHP.

不,正如其他人所说,“PHP 中没有指针”。我补充说,PHP 中没有与 RAM_相关的内容。

And also all answers are clear. But there were points being left out that I could not resist!

而且所有的答案都很清楚。但有些地方被遗漏了,我无法抗拒!

There are number of things that acts similar to pointers

有许多行为类似于指针的东西

  • evalconstruct (my favorite and also dangerous)
  • $GLOBALSvariable
  • Extra '$' sign Before Variables (Like prathkmentioned)
  • References
  • eval构造(我最喜欢的也是危险的)
  • $GLOBALS变量
  • 变量前的额外“$”符号(如提到的prathk
  • 参考


First one

第一

At first I have to say that PHP is really powerful language, knowing there is a construct named "eval", so you can create your PHP code while running it! (really cool!)

首先我不得不说 PHP 是一种非常强大的语言,知道有一个名为“eval”的构造,因此您可以在运行它的同时创建您的 PHP 代码!(真的很酷!)

although there is the danger of PHP_Injection which is far more destructive that SQL_Injection. Beware!

尽管存在比 SQL_Injection 更具破坏性的 PHP_Injection 危险。谨防!

example:

例子:

Code:

代码:

$a='echo "Hello World.";';
eval ($a);

Output

输出

Hello World.

你好,世界。

So instead of using a pointer to act like another Variable, You Can Make A Variable From Scratch!

因此,您可以从头开始制作一个变量,而不是使用指针来充当另一个变量!



Second one

第二个

$GLOBALvariable is pretty useful, You can access all variables by using its keys.

$GLOBAL变量非常有用,您可以使用其键访问所有变量。

example:

例子:

Code:

代码:

$three="Hello";$variable=" Amazing ";$names="World";
$arr = Array("three","variable","names");
foreach($arr as $VariableName)
    echo $GLOBALS[$VariableName];

Output

输出

Hello Amazing World

你好神奇世界

Note:Other superglobalscan do the same trick in smaller scales.

注意:其他超全局变量可以在较小的范围内执行相同的技巧。



Third one

第三个

You can add as much as '$'s you want before a variable, If you know what you're doing.

如果您知道自己在做什么,您可以在变量前添加任意数量的 '$'。

example:

例子:

Code:

代码:

$a="b";
$b="c";
$c="d";
$d="e";
$e="f";

echo $a."-";
echo $$a."-";   //Same as $b
echo $$$a."-";  //Same as $$b or $c
echo $$$$a."-"; //Same as $$$b or $$c or $d
echo $$$$$a;    //Same as $$$$b or $$$c or $$d or $e

Output

输出

b-c-d-e-f

定义



Last one

最后一个

Reference are so close to pointers, but you may want to check this linkfor more clarification.

参考与指针非常接近,但您可能需要查看此链接以获得更多说明。

example 1:

示例 1:

Code:

代码:

$a="Hello";
$b=&$a;
$b="yello";
echo $a;

Output

输出

yello

黄色的

example 2:

例子2:

Code:

代码:

function junk(&$tion)
{$GLOBALS['a'] = &$tion;}
$a="-Hello World<br>";
$b="-To You As Well";
echo $a;
junk($b);
echo $a;

Output

输出

-Hello World

-To You As Well

-你好,世界

-你也是

Hope It Helps.

希望能帮助到你。

回答by Anthony

That syntax is a way of accessing a class member. PHP does not have pointers, but it does have references.

该语法是访问类成员的一种方式。PHP 没有指针,但它有引用。

The syntax that you're quoting is basically the same as accessing a member from a pointer to a class in C++ (whereas dot notation is used when it isn't a pointer.)

您引用的语法与在 C++ 中从指向类的指针访问成员基本相同(而当它不是指针时使用点表示法。)

回答by Toby Hede

To answer the second part of your question - there are no pointers in PHP.

回答问题的第二部分 - PHP 中没有指针。

When working with objects, you generally pass by reference rather than by value - so in some ways this operates like a pointer, but is generally completely transparent.

在处理对象时,您通常通过引用而不是通过值传递 - 因此在某些方面它像指针一样操作,但通常是完全透明的。

This does depend on the version of PHP you are using.

这确实取决于您使用的 PHP 版本。

回答by Rich

entryId is an instance property of the current class ($this) And $entryId is a local variable

entryId 是当前类的实例属性 ($this) 而 $entryId 是局部变量

回答by Peter

You can simulate pointers to instantiated objects to some degree:

您可以在某种程度上模拟指向实例化对象的指针:

class pointer {
   var $child;

   function pointer(&$child) {
       $this->child = $child;
   }

   public function __call($name, $arguments) {
       return call_user_func_array(
           array($this->child, $name), $arguments);
   }
}

Use like this:

像这样使用:

$a = new ClassA();

$p = new pointer($a);

If you pass $p around, it will behave like a C++ pointer regarding method calls (you can't touch object variables directly, but that's evil anyways :) ).

如果你传递 $p ,它的行为就像一个关于方法调用的 C++ 指针(你不能直接接触对象变量,但这无论如何都是邪恶的 :) )。

回答by prathk

Yes there is something similar to pointers in PHP but may not match with what exactly happens in c or c++. Following is one of the example.

是的,有一些类似于 PHP 中的指针的东西,但可能与 c 或 c++ 中发生的事情不匹配。以下是示例之一。

$a = "test";
$b = "a";
echo $a;
echo $b;
echo $$b;

//output
test
a
test

This illustrates similar concept of pointers in PHP.

这说明了 PHP 中指针的类似概念。

回答by prathk

PHP passes Arrays and Objects by reference (pointers). If you want to pass a normal variable Ex. $var = 'boo'; then use $boo = &$var;.

PHP 通过引用(指针)传递数组和对象。如果你想传递一个普通的变量 Ex。$var = '嘘'; 然后使用 $boo = &$var;。

回答by user319072

PHP can use something like pointers:

PHP 可以使用类似指针的东西:

$y=array(&$x);

Now $y acts like a pointer to $x and $y[0] dereferences a pointer.

现在 $y 就像一个指向 $x 的指针,而 $y[0] 取消引用一个指针。

The value array(&$x) is just a value, so it can be passed to functions, stored in other arrays, copied to other variables, etc. You can even create a pointer to this pointer variable. (Serializing it will break the pointer, however.)

值数组(&$x) 只是一个值,因此它可以传递给函数、存储在其他数组中、复制到其他变量等。您甚至可以创建指向该指针变量的指针。(但是,序列化它会破坏指针。)