OOP PHP,使用魔术方法的getter和setter方法 [英] OOP PHP, getters and setters using magic methods

查看:89
本文介绍了OOP PHP,使用魔术方法的getter和setter方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以为PHP类的私有成员创建访问器和更改器的哪些策略?这个建议好吗: http://cormacscode.wordpress.com/2009/01/22/read-only-object-variables-in-php-using-magic-methods/

What strategies are available to creating accessors and mutators for a PHP class' private members? Is this suggestion good: http://cormacscode.wordpress.com/2009/01/22/read-only-object-variables-in-php-using-magic-methods/

<?php
class classWithReadOnlyVar
{
    private $readOnlyVar;

    public function __get($varName)
    {
        return $this->$varName;
    }

    public function __set($varName,$varValue)
    {
    }
}

如果某些成员需要私有,公共或受保护的财产方法怎么办?

What if some members needed a private, public, or protected property method?

推荐答案

首先,定义__get__set等,public,否则就不能使用它们.应该明智地使用魔术方法,因为调用魔术方法所花的时间大约是简单地调用类方法的时间的三倍.

First of all, __get, __set, etc. are defined public and you cannot have them otherwise. Magic methods should be used wisely as it takes about three times as long to make a call to a magic method than simply calling a class method.

class A {
   public function __get($name) { ... }

   public function __getValue() { ... }     // <== is faster

}

通常(通常,最好是),您将拥有类成员privateprotected(从不public),并具有访问器和变异器来封装它们.这些访问器和更改器可能具有任何可见性,具体取决于用户对成员的处理方式.通过仅声明成员的访问器,也可以使它们具有不可变的类,这些访问器仅在构造函数中初始化.

Usually (normally, preferably), you will have your class members private or protected (never public) and have accessors and mutators to encapsulate them. Those accessors and mutator may be of any visibility, depending on what the user can do with the members. You may also have immutable classes by declaring only accessors for your members, which are initialized in the constructor only.

因此,您的示例课应阅读

So, your sample class should read

class classWithReadOnlyVar {
   private $readOnlyVar;

   public function getReadonlyVar() {
     return $this->readOnlyVar;
   }

}

并且不应使用魔术方法.

and should not use the magic methods.

可能有很多原因完全避免使用魔术方法:

There may be many reasons to avoid using magic methods at all :

  1. 他们的中断代码完成
  2. 它们在运行时速度较慢
  3. 它们使重构和维护变得更加困难/复杂
  4. 您不能使用protected魔术方法
  1. they break code completion
  2. they are slower at run-time
  3. they make refactoring and maintenance a bit (lot) harder/complicated
  4. you can't have a protected magic method
  5. etc.


班级成员

它们的可见性应该为privateprotected,这完全取决于您是否希望通过继承对其进行访问.它们永远不应为public,因为这会破坏OO范式.

Their visibility should be private or protected, it all depends if you want them accessible by inheritence. They should never be public as this breaks the OO paradigm.

受保护成员的示例:

class A {
    protected $_name = 'A';

    public function getName() { return $this->_name; }
}

class B {
    protected $_name = 'B';   // getName() will not return 'B'
}

(没有$_nameprotected,这是不可能的,并且不需要重新定义访问器)

(without $_name being protected, this would not be possible, and no need to redefine the accessor)

访问者

它们应该是protectedpublic.拥有private访问器没有任何意义.一个类应该直接访问它的成员.如果成员需要处理,则该类将知道何时调用该访问器.

They should be protected or public. Having a private accessor makes no sense; a class should access it's member directly. If the member needs processing, the class will know regardless when to call the accessor or not.

Mutators

它们应该是protectedpublic.对于访问器,没有private突变器是没有意义的,除非在极少数情况下需要在内部进行处理.如果类成员没有访问器,则它应该具有变量.在没有办法以某种方式取回值的情况下,没有办法设置一个值也是没有意义的.

They should be protected or public. As for the accessors, it makes no sense to have a private mutator... unless a very rare case when processing needs to be done internally. If a class member has no accessor, it should not have a mutator. It also makes no sense to have a mean to set a value without being able to get the value back somehow.

这篇关于OOP PHP,使用魔术方法的getter和setter方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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