PHP类def:使用switch()的个别访问器/ mutator或__set()? [英] PHP class def: Individual accessors/mutators or __set() with switch()?

查看:140
本文介绍了PHP类def:使用switch()的个别访问器/ mutator或__set()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义PHP类时,哪个是首选/最佳实践?



看起来它可以更干净,简洁和方便的写一个 __ set() 魔法方法,并在其中构建一个 switch()与所有我想允许访问的私有成员的情况。它不会从类中自动调用,但是再也不会 setFoo(),所以如果我想在内部使用访问器/ mutator,我会有以显式调用方法。



另一个区别是在类外的代码中,我总是可以以与<$ c $相同的方式访问成员var与使用许多单独的方法相比,c> $ obj-> foo ,无论是public(直接)还是private(使用 __ set() / p>

我想这主要是因为美学的选择。例如,如果我有一个购买的地址数据,我不想有16个或更多单独的访问方法只是为名字,姓,地址1,地址2,城市,州等每个运费和计费数据。



我忽略了哪些关键差异? (可能是一个复杂的IDE拒绝自动完成成员名称之外的类,因为它被标记为私有?)我几乎回答了我自己的原始问题?预先感谢您的输入。

解决方案

与每个想要从外部访问的成员访问各个访问者。我试过两个,发现这些原因使用访问器:




  • 无论你使用什么来记录你的API(doxygen / PHPdoc / Zend)生成的文档不会显示可通过魔法函数访问的成员。

  • 您可以记录访问器。你真的应该能够在文档中添加一行:重要!这个函数连接到数据库,它会真的很慢,如果可以使用otherFunction()。

  • 访问器的实现对任何人都很容易看到。我不想深入了解一个200行的魔术函数的细节,以检查除了设置/获取值之外的访问器是否有什么(这就是为什么我们编写访问器)。

  • 您已经提到了IDE的自动完成功能。

  • __get()函数具有定义良好的函数头,因此您将无法创建返回引用的getter,例如(使用数组时非常棒,即 $ numbers =& $ object-> getNumbers(); $ numbers [] = 4; 参考,您将需要再次调用setter。)


When defining a PHP class, which is preferred/best practice? Are there any key differences I'm overlooking?

It seems like it could be more clean, concise, and convenient to write a __set() magic method and put a switch() construct in it with cases for all the private members to which I want to allow access. It wouldn't be called automagically from inside the class, but then again neither would setFoo(), so if I want to use the accessor/mutator internally, I'd have to explicitly call a method either way.

The other difference is that in the code outside the class, I could always access member vars in the same fashion as $obj->foo, whether public (directly) or private (using __set()), versus using many separate methods.

I guess this comes down mostly to an aesthetic choice. For example, if I have address data on a purchase, I don't want to have 16 or more separate accessor methods just for first name, last name, address1, address2, city, state, etc. each for shipping and billing data.

Are there any key differences I've overlooked? (Might a sophisticated IDE refuse to auto-complete a member name outside the class because it's marked as private?) Have I pretty much answered my own original question? Thanks in advance for your input.

解决方案

Go with the individual accessors for each and every member you want to be accessible from outside. I have tried both and found these reasons to use the accessors:

  • Whatever you use to document your API (doxygen/PHPdoc/Zend), the generated docs won't show the members that are accessible through magic functions.
  • You can document the accessors. You really ought to be able to put a line like this in the documentation: "IMPORTANT! This function connects to the database, it will be really slow, use otherFunction() if you can."
  • The implementation of the accessor is easily visible for anyone. I wouldn't want to dig into the details of a 200-line magic function to check if the accessor does anything besides setting/getting the value (that's why we're writing accessors after all.)
  • You already mentioned the autocompletion of IDEs.
  • The __get() function has a well-defined function header, so you won't be able to create getters that return a reference, for example (which is really great when working with arrays, i.e. $numbers = &$object->getNumbers(); $numbers[] = 4; - without the reference, you would need to call the setter again.)

这篇关于PHP类def:使用switch()的个别访问器/ mutator或__set()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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