简体   繁体   English

PHP-在函数返回中增加变量

[英]PHP - Increment a variable in a function return

Here are 2 versions of a very simple class. 这是一个非常简单的类的2个版本。

Only the getCounterIncrement() function is different in the 2 versions. 在两个版本中,只有getCounterIncrement()函数有所不同。

Version 1 版本1

class Counter {

  protected $counter;

  public function __construct() {

    $this->counter = 0;
  }

  public function getCounterIncrement() {

    return $this->counter++;
  }
}

$counter = new Counter;
print $counter->getCounterIncrement(); // outputs 0
print $counter->getCounterIncrement(); // outputs 1

Version 2 版本2

class Counter {

  protected $counter;

  public function __construct() {

    $this->counter = 0;
  }

  public function getCounterIncrement() {

    $this->counter++;
    return $this->counter;
  }
}

$counter = new Counter;
print $counter->getCounterIncrement(); // outputs 1
print $counter->getCounterIncrement(); // outputs 2

Questions 问题

  • Why is the output different in the 2 versions? 为什么两个版本的输出不同?
  • Is one of the 2 versions better than the other in terms of coding standards? 就编码标准而言,这两个版本中的一个是否比另一个更好?
  • Would there be a nicer / better way to code that? 会有更好/更好的方法来编码吗?

From PHP official documentation : PHP官方文档

++$a : Pre-increment => Increments $a by one, then returns $a. ++ $ a:预递增=>将$ a递增1,然后返回$ a。

$a++ : Post-increment => Returns $a, then increments $a by one. $ a ++:后递增=>返回$ a,然后将$ a递增1。

In your first version, you first return the value, then increment it ( post-increment ). 在第一个版本中,您首先返回该值,然后将其递增( post-increment )。 So if you want to return the incremented value, you have to use the pre-incrementation : 因此,如果要返回增加的值,则必须使用pre-incrementation

public function getCounterIncrement() {
    return ++$this->counter;
}

Standard case of ++ being after or before the variable name. ++标准情况是在变量名之后或之前。 If it comes after the variable name you get the previous value returned as 0 in this case. 如果在变量名之后,它会返回以前的值,在这种情况下为0。 When you have it after your variable you increment it first and return that new value. 如果在变量后添加变量,则首先将其递增,然后返回该新值。

Neither of them is a better or worse way, its just two different operators which can be used in different cases as need be. 它们都不是更好更坏的方法,它只是两个不同的运算符,可以根据需要在不同的情况下使用。 Post-Increment and Pre-Increment they are called and same for Decrement. Post-IncrementPre-Increment分别称为递减。

PHP Incrementing/Decrementing Operators PHP递增/递减运算符

I would probably re-write it a little bit (even though this is no better way) like this 我可能会像这样重写一点(即使这不是更好的方法)

<?php

class Counter {
  protected $counter;

  public function __construct() {
    $this->counter = 0;
  }

  public function incrementValue()
  {
    $this->counter++;
  }
  public function getValue() {
    return $this->counter;
  }
}

$counter = new Counter;
$counter->incrementValue();
print $counter->getValue();

PS: Looking at your Profile I think you asked this question for fun? PS:查看您的个人资料,我想您问这个问题很有趣吗?

In this line you increase the counter after returning the counter: 在此行中,您需要在返回计数器后增加计数器:

return $this->counter++;

Instead you need to increase its value before returning, like this: 相反,您需要在返回之前增加其值,如下所示:

return ++$this->counter;

例如,当您说i ++意味着使用i,然后将其增加1。因此,在第二个版本中,它先增加它,然后返回它,这就是输出不同的原因。

Why is the output different in the 2 versions? 为什么两个版本的输出不同?

Because in first version what is does is, it uses the value first and then increment the same. 因为在第一个版本中是这样做的,所以它先uses该值,然后再递增该值。 That's why in first version it print 0,1 and the second version 1,2. 这就是为什么在第一个版本中它打印0,1而第二个版本1,2中的原因。

  • In first case it returns the value first and then increment it. 在第一种情况下,它首先返回该值,然后将其递增。
  • In second case it increments the value first and then return it. 在第二种情况下,它会先增加值,然后再返回。

Is one of the 2 versions better than the other in terms of coding standards? 就编码标准而言,这两个版本中的一个是否比另一个更好?

No, both are good in terms of coding standard. 不,在编码标准方面都不错。 It all depends on the implementation you want to use. 这完全取决于您要使用的实现。

Would there be a nicer / better way to code that? 会有更好/更好的方法来编码吗?

It's already a nice way in you've written. 这已经是您写作的好方法。 It's just about choosing the method you like/want. 这只是选择您喜欢/想要的方法。

  1. Question: ++ after a variable means that the increment will happen after any code execution. 问题:变量后的++表示增量将在执行任何代码后发生。

In your 1st option it returns the current value first and then increment. 在您的第一个选项中,它首先返回当前值,然后递增。

In your 2nd option it will update in the first line, and in the next line return the updated value. 在您的第二个选项中,它将在第一行中更新,然后在下一行中返回更新后的值。

Hope you understand what I said. 希望你能理解我的意思。

2 and 3 Questions: You can use the ++ before the variable so that it updates and then return the value. 2和3问题:您可以在变量前使用++,以便对其进行更新,然后返回该值。 But I don't see any problem in your code. 但我在您的代码中看不到任何问题。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM