简体   繁体   English

PHP OOP-直接修饰属性是好是坏?

[英]PHP OOP - Is it a good/bad directly adressing properties?

I will try to show this by an example: 我将尝试通过一个例子来说明这一点:

class DogShelter
{
  private $dog;

  public function handleDogDirect( )
  {
     $this->dog = trim( $this->dog );
     $this->dog = rtrim( $this->dog, 'abc' );
     $this->dog = strtolower( $this->dog );
     $this->dog = basename( $this->dog );
  }

  public function handleDogIndirect( )
  {
     $dog = $this->dog;

     $dog = trim( $dog );
     $dog = rtrim( $dog, 'abc' );
     $dog = strtolower( $dog );
     $dog = basename( $dog );

     $this->dog = $dog;
  }
}

Which is the better function in different cases - handleDogDirect or handleDogIndirect ? 在不同情况下,哪个函数更好handleDogDirecthandleDogIndirect

  • Why? 为什么?
  • Which one is probably faster? 哪一个可能更快?
  • Which one should I use? 我应该使用哪一个?

By the way: Since the rep recalc, I can't login with my OpenID from wordpress anymore. 顺便说一句:由于代表重新计算,所以我无法再通过wordpress使用OpenID登录。 It keeps saying No OpenID endpoint found. 它一直说No OpenID endpoint found. . I would really appreciate any help. 我真的很感谢您的帮助。

I would use handleDogDirect since it's accessing the instance of the $dog which seems to be what you wanted to achieve. 我将使用handleDogDirect因为它正在访问$dog 实例 ,这似乎是您想要实现的目标。 The latter function seems to create an unnecessary variable to perform the same function. 后一个函数似乎创建了不必要的变量来执行相同的功能。

As a convention whenever I am using properties of the class I am within inside class functions I always use $this keyword directly. 按照惯例,每当我使用类的属性时,我就在类函数内部,我总是直接使用$this关键字。

If there is a performance issue with either of the two, its probably negligible. 如果两者中的任何一个都存在性能问题,则它可以忽略不计。 I strongly suggest to worry about performance issues when it's time to worry about performance. 强烈建议在需要考虑性能时考虑性能问题。

I don't think there's a point in creating a new variable for no reason. 我认为无缘无故创建一个新变量毫无意义。 I think the performance hit if any is negligible. 我认为对性能的影响可以忽略不计。 What should actually happen is that before the value is placed in the class variable it should be clean up ie within setDog(). 实际上应该发生的是,在将值放入类变量之前,应将其清除,即在setDog()中。

I would say this is more an issue of personal coding style. 我会说这更多是个人编码风格的问题。

I sometimes do it like handleDogIndirect( ) because it makes more sense to me the way I visualize my code, becuase it involves less typing (especially less holding down of the shift key), and because I think it reads better. 有时我会像handleDogIndirect()那样进行操作,因为它对我可视化代码的方式更有意义,因为它涉及的键入次数更少(尤其是按住shift键的次数更少),并且因为我认为它读起来更好。

I'll try to explain why it makes more sense to me. 我将尝试解释为什么这对我来说更有意义。 The functions are their own little world, that's why they have their own scope. 这些功能是他们自己的小世界,这就是为什么它们拥有自己的范围。 They're happy when they work in that scope, so I first want to make sure that I pull everything they will need into their scope. 他们在该范围内工作时很高兴,因此我首先要确保我将他们需要的所有物品都拉到他们的范围内。 Once that's done I don't have to think about what's going on anywhere else, only what's local to that function. 完成此操作后,我不必考虑其他任何地方发生的事情,只需考虑该函数的本地情况即可。 Less mind clutter. 减少思维混乱。

Then when they're done doing their work I put their useful data back where it belongs. 然后,当他们完成工作时,我会将他们的有用数据放回原处。

I think it especially helps if you have a method that's somewhat long or complicated. 如果您使用的方法过长或过于复杂,我认为这特别有帮助。

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

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