PHP 5.x 中的引用传递

发布于 2024-08-07 17:15:25 字数 87 浏览 7 评论 0原文

我正在学习 Zend PHP5 认证,一切看起来都不错,但我找不到传递或返回我的参考变量的真实示例。

如果有人能举例说明何时使用它,那就太好了?

I am studying for the Zend PHP5 Certification, everything looks good but i can not find real world examples for passing or returning variables my reference.

It would be very nice, if someone has an example when to use this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城月光淡如水﹏ 2024-08-14 17:15:25

假设您要编写一个函数来从数组中删除某些值。

function remove_elements($array, $item1, $item2, ...) {
  ...
}

您可以复制数组并返回它。但是如果您想知道是否有任何内容被删除怎么办?相反,您就地修改数组并返回删除的元素数。因此:

function remove_elements(&$array) {
  // remove elements
  return $number_removed;
}

如果您在函数内创建一个复杂的结构,您可能希望返回对它的引用而不是它的副本。但这是一个相当边缘的情况,因为 PHP 使用写时复制(即在修改之前不会复制),但有一些有效的用例。

当您编写类的成员函数时,通过引用返回更有意义。您可以返回对数据成员的引用,但这也会破坏封装。

最后,值得注意的是,默认情况下所有对象都是通过引用传递和返回的。其他一切都按值传递和返回。

Let's say you want to write a function that removes certain values from an array.

function remove_elements($array, $item1, $item2, ...) {
  ...
}

You could copy the array and return it. But what if you want to know if anything was removed? Instead you modify the array in-place and return the number of elements removed. So:

function remove_elements(&$array) {
  // remove elements
  return $number_removed;
}

If you create a complex structure inside your function you may want to return a reference to it rather than a copy of it. But this is a fairly marginal case because PHP uses copy-on-write (ie its not copied until it's modified) but there are some valid use cases.

Returning by reference makes more sense when you're writing a member function of a class. You could return a reference to a data member but this can also break encapsulation.

Lastly, it's worth noting that all objects are passed and returned by reference by default. Everything else is passed and returned by value.

空城仅有旧梦在 2024-08-14 17:15:25

通过引用传递:类似 asort() 的函数

$array = array(3, 2, 1);
asort($array);

Pass by reference: functions like asort()

$array = array(3, 2, 1);
asort($array);
只怪假的太真实 2024-08-14 17:15:25

php5中的对象默认是通过引用传递的。 (有关正确的描述,请参阅 http://docs.php.net/language.oop5.references< /a>)

通过引用传递参数已经得到解答...

作为通过引用返回值的(现实世界)示例,选择任何 流畅的接口,其中方法返回对同一对象的引用作为其自己的调用上下文($this)。如果返回值是副本/克隆,则这将不起作用(如预期)。

例如,

class MyClass {  
  protected $foo = 'n/a';
  protected $bar = 'n/a';

  function foo($val) {
    $this->foo = (int)$val;
    return $this;
  }

  function bar($val) {
    $this->bar = (int)$val;
    return $this;
  }

  function __toString() {
    return 'foo='.$this->foo.' bar='.$this->bar;
  }
}

$o = new MyClass;
$o->foo(1)
  ->bar(2);
echo $o;

这会打印 foo=1 bar=2 ,我认为这就是人们所期望的结果,这里并不奇怪。
如果该方法按值返回 $this,则它将不再起作用。您可以通过强制 php 返回副本来测试这一点。而不是使用 return $this

return clone $this;

,输出将是

foo=1 bar=n/a

(我不会称该界面直观;-))

Objects in php5 are passed by reference by default. (For a correct description see http://docs.php.net/language.oop5.references)

Passing arguments by reference has already been answered...

As a (real world) example for a return value by reference pick any fluent interface where a method returns a reference to the same object as its own call-context ($this). This wouldn't work (as expected) if the return value was a copy/clone.

E.g.

class MyClass {  
  protected $foo = 'n/a';
  protected $bar = 'n/a';

  function foo($val) {
    $this->foo = (int)$val;
    return $this;
  }

  function bar($val) {
    $this->bar = (int)$val;
    return $this;
  }

  function __toString() {
    return 'foo='.$this->foo.' bar='.$this->bar;
  }
}

$o = new MyClass;
$o->foo(1)
  ->bar(2);
echo $o;

This prints foo=1 bar=2 and I think that's the result one would expect, no surprises here.
If the method returned $this by value, it wouldn't work anymore. You can test that by forcing php to return a copy. Instead of the return $this lines use

return clone $this;

and the output will be

foo=1 bar=n/a

(and I wouldn't call that interface intuitive ;-))

明明#如月 2024-08-14 17:15:25

如果您与 chainig 一起工作,它可能会很有用,例如:

    public function addObject(array &$someData){
$newObj = Fabric::create($someData);
$this->listOfObj[] = $newObj;
return $newObj;
}

....
$this->addObj(array('idontknow' => 'anything'))->setName('mr.knowitall')->save();

... Sry。与 Volkers 相同:((流畅的接口就像我所知道的那样链接)。

想象一下,您有一个带有加载方法的流畅接口。加载接口替换或添加一些数据。在这里,通过调用进行操作可能很有用参考值$success判断加载成功


采取一些具有行列表的数据库对象。 Nr。行数指向所有对象的右侧数据(parallesl)。如果您返回 nr。并将其传递给下一个对象,您可以跳过一个对象中的行并跳过其他对象。总是有一堆有效的对象。

It could be useful, if you work with chainig,like:

    public function addObject(array &$someData){
$newObj = Fabric::create($someData);
$this->listOfObj[] = $newObj;
return $newObj;
}

....
$this->addObj(array('idontknow' => 'anything'))->setName('mr.knowitall')->save();

... Sry. Same as Volkers :( (The Fluent Interface is Chaining like i know it).

Image you have a fluent Interface with an load-method. The Load-interface replace or adds some data. Here it could be useful to act with an call by reference value $success to determine that the load was successful.

Return by reference:
Take some database-objects having a list of rows. The Nr. of rows points to right data of all objects (parallesl). If you return the nr. and pass it to the next object, you can skip the row in one object and skip in the others too. Always having a valide bunch of objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文