如何避免魔术字符串(PHP MVC 框架)
我想将 MVC 框架与 PHP 结合使用,以很好地分离代码和表示。我目前已经开始研究 CakePHP。看起来不错,但是那些神奇的弦是怎么回事?
看一下下面的代码(摘自 CakePHP 食谱):
class User extends AppModel {
var $name = 'User';
var $hasOne = 'Profile';
var $hasMany = array(
'Recipe' => array(
'className' => 'Recipe',
'conditions' => array('Recipe.approved' => '1'),
'order' => 'Recipe.created DESC'
)
);
}
那些神奇的字符串让我很困扰。代码中的字符串应该只是要输出的文本!
“Recipe.created DESC”中存在一个拼写错误,它无法按预期工作。这里没有智能感知/代码完成可以提供帮助!
另外,如果我想将“Recipe”重新设置为其他内容怎么办?我必须手动搜索所有代码并查明它是常规文本还是魔术字符串之一,
其他 PHP MVC 框架是否更好?(阅读:更少或没有魔术字符串)
有关如何避免魔术字符串的任何链接(至少为)尽可能)...?
I want to use a MVC-framework together with PHP to make a nice separation between code and presentation. I've currently started to look at CakePHP. It looks nice, but whats about all those magic strings?
Take a look at the code below (taken from the CakePHP cookbook):
class User extends AppModel {
var $name = 'User';
var $hasOne = 'Profile';
var $hasMany = array(
'Recipe' => array(
'className' => 'Recipe',
'conditions' => array('Recipe.approved' => '1'),
'order' => 'Recipe.created DESC'
)
);
}
It bothers me with those magic strings. Strings in code ought only to be text to be outputted!
One spelling mistake in 'Recipe.created DESC" and it won't work as expected. There's no intellisense/code completion to help here!
Also, what if I want to remane 'Recipe' to something else? I have to search manually through all the code and find out if it's regular text or one of the magic strings.
Are other PHP MVC-frameworks better? (read: less or no magic strings)
Any links on how to avoid magic strings (at least as much as possible)...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Cake 遵循“约定优于配置”的方法。它是一个可用于快速原型化和部署(大部分)基于 CRUD 的应用程序的框架。它在幕后发生了很多“魔法”,如果您不知道幕后发生了什么,那么它并不总是一个好的选择。
至于你的具体例子,还不错。 Recipe实际上指的是与
User
模型相关的Recipe
模型。 Cake 有一个内置的 ORM,它使用一些模型变量来正确设置模型关系。我想说,任何 ORM 框架都是如此。除非更改类名,否则不需要更改所有引用,这在任何 PHP 代码中都是如此。至于其他框架建议。我建议您在开始使用框架之前先使用自己的 PHP MVC 堆栈。如果您对 PHP 有一定的了解,您可以查看 CodeIgniter 或 Kohona 框架。它们的刚性不如蛋糕。您还可以查看 Zend Framework。还有许多其他框架具有不同的功能(或多或少提供不同的控制变化),您所需要做的就是环顾四周。
Cake follows the 'Convention over Configuration' methodology. It's a framework that can be used to rapidly prototype and deploy (mostly) CRUD based applications. It's got a lot of 'magic' happening behind the scenes which don't always make it a good choice if you don't know what's going on under the hood.
As to your specific example, it isn't too bad. Recipe actually refers to a
Recipe
model that theUser
model is related to. Cake has an inbuilt ORM that uses some model variables to set your model relations properly. This would be the case in any ORM framework, I'd say. Unless you changed the class name, you wouldn't need to change all the references, this would be true in any PHP code.As to other framework recommendations. I'd suggest that you work with your own PHP MVC stack before you begin to work with frameworks. If you have a fair understanding of PHP, you could look at the CodeIgniter or Kohona framework. These are less rigid than Cake. You could also have a look at the Zend Framework. There are many other frameworks out there that have different features (more or less with differing variations of control offered), all you need to do is look around.
您正在寻找的是好的 ORM。尝试使用以下之一:
有关 ORM 的更多信息:
http://en.wikipedia.org/wiki/Object-relational_mapping
What you are searching is the good ORM. Try using one of these:
More about ORM:
http://en.wikipedia.org/wiki/Object-relational_mapping
我看到你在那里做了什么
无论您选择哪个 ORM 库,总会有魔法字符串。只是没有足够的信息来自动将您的类映射到数据库表(除了最简单的情况)。魔法字符串实际上是 ORM 创建映射所需的元数据。
您可以尝试通过定义常量或创建公共静态字段来避免魔术字符串,但最终您最多只能将它们合并到单个文件中。
当使用 ORM 时,无论如何你都必须提供元数据。考虑到这一点,您应该选择让您感觉最舒服的 ORM。
I see what you did there
No matter which ORM library you choose there will always be magic strings. There just isn't enough information to automatically map your class to a database table (except in the simplest cases). The magic strings are really meta-data that the ORM needs to create the mapping.
You can try to avoid magic strings by defining constants or creating public static fields, but ultimately you will have only consolidated them to, at best, a single file.
When working with an ORM you will have to provide meta-data no matter what. With that in mind, you should choose the ORM that feels most comfortable to you.