PHP 和编写干净的代码
我正在尝试寻找编写 PHP 的最佳实践。
我只是想知道这是一个坏习惯吗?
例如,处理变量。
$var = 1
$var = doSomething($var);
$var = doSomething2($var);
$var = doSomething3($var);
看起来有点可怕。
这是我刚刚编写的真实代码的示例:
$this->rSum = explode(",", $this->options["rSum"]);
$this->rSum = array_combine(array_values($this->rSum), array_fill(0, count($this->rSum), 0));
如果有人可以传给我一些关于编写更清晰的代码的好教程,那就太好了!
我又问愚蠢的问题了。 :)
顺便问一下..
模型中可以有多少自动处理?
我有一个具有执行方法的模型,当我调用它时,它会执行很多操作,例如读取定义文件和进行数据库查询。
例如
$object = new Object()
$object->setFile("example.txt");
$object->execute();
// Then i can fetch things from it
echo $object->getName();
Im trying to find best practices to write PHP.
I just wonder is this a bad habit.
For example, processing variables.
$var = 1
$var = doSomething($var);
$var = doSomething2($var);
$var = doSomething3($var);
It looks a bit awful.
Here is a example of a real code that I just did:
$this->rSum = explode(",", $this->options["rSum"]);
$this->rSum = array_combine(array_values($this->rSum), array_fill(0, count($this->rSum), 0));
If someone could pass me some good tutorials of writing cleaner code generally it would be nice!
Its me again asking stupid questions. :)
By the way..
How much automatic processing can there be in models?
I have a model that has a execute method and when I call it, it does a lot of things like reading a definition file and making database queries.
For example
$object = new Object()
$object->setFile("example.txt");
$object->execute();
// Then i can fetch things from it
echo $object->getName();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
除了编码标准之外,您还可以使用 PHP_CodeSniffer 获取有关现有代码的一般提示。
In addition to the coding standards, you can use PHP_CodeSniffer to get general hints on your existing code.
同意 Jim Li 的观点,我也更喜欢可读的代码,而不是微优化或智能但丑陋的单行代码。
我对你的第一个例子遇到的唯一麻烦是它使用面向过程的函数,我最好重写OOP(像这样的函数调用可以链接起来并保持易于阅读)。
PHP 没有很强的 OOP 目标,因为它在 API 中主要使用过程语句。但我更喜欢以面向对象的方式编写代码,良好地解耦和组织,而不是使用大量带有大量参数的函数来让它们一起工作。
Agreed with Jim Li, I also prefer readable code over micro-optimisation or smart but ugly one-liner code.
The only trouble I had with your first example is that it uses procedural oriented functions, I would be better rewritten OOP (functions calls like this could be chained and stay easy to read).
PHP hasn't a strong OOP aim, as it uses mainly procedural statements in its API. But I prefer writing my code in OO way, well decoupled and organisated, rather than having a whole lot of functions with plenty of arguments to get them work together.
尝试在一行中完成所有操作可能导致的一件事是代码假设。当我必须不断修复它们时,我觉得这真的很烦人。这在对象链中更常见。例如,
很多人会告诉你,它更容易阅读;然而它依赖于每次返回都是一个对象。我更喜欢返回每一个并检查响应。
当然,它需要更多的击键,但它不太可能爆炸,而且我认为无论如何,更容易阅读。
尽管鲍里斯·盖里(Boris Guéry)所写的内容值得重复。 保持一致。
One thing that trying to do eveything in one line can lead to is code assumptions. Which I rate as really annoying when I have to keep fixing them. This is more common with object chaining. For example
A lot of people will tell you that it is easier to read; however it relies on each return being an object every time. I prefer to return each one and check the response.
It takes more keystrokes sure, but it will be less likely to blow up and, I think anyway, easier to read.
Although it is worth repeating what Boris Guéry wrote. be consistent.
在我看来,智能代码不一定是好代码。我个人更喜欢干净、简单且易于理解的代码。你的 2 行代码会让你的同行认真思考,而不是你的 init“坏”代码。
无论如何,这只是我的看法
Smart code is not necessarily good code in my opinion. I'd personally prefer clean, simple and easy to understand code. Your 2 liner will make your peer think hard, as opposed to your init "bad" code.
That's just my take anyway
最重要的是,保持一致。
The most important, be consistent.
我真的很喜欢你的(真实的)代码,而且我通常很难喜欢其他人的代码(我没有太多时间深入研究 ZF,但例如 PEAR [他们也有自己的编码标准] 在我看来就很糟糕),您给出的第一个示例似乎很愚蠢,但关于第二个示例,至少对我来说真的很容易理解,并且从您提供的简短片段来看,您似乎具有一致的编码风格,并且在正确的位置使用正确数量的空格地方 - 这对于干净的代码来说非常重要(如果你不相信我,只需看看一些 Perl 片段)。
我只想指出三件事:
rSum
对于属性来说并不是一个糟糕的名称,但不太清楚它包含什么值,也许您可以为该属性找到一个更具描述性的名称吗?如果您使用正确的函数,您的第二个“真实”示例可能会更干净、更快:
$this->rSum = array_flip(explode(",", $this->options["rSum"]));
编辑:我刚刚注意到我上面提供的代码并不完全是你正在做的事情(
0
没有被我的大脑处理),这是另一个可行的替代方案:有似乎这里有很多人不喜欢单调,但我相信上面的代码是清晰、高效和描述性的 - 但这可能只是我......=)
I really like your (real) code and it's usually kinda hard for me to like other persons code (I haven't had much time to dig into ZF but PEAR for instance [they also have their coding standards] is just awful IMO), the first example you gave just seems stupid but regarding the second one, it is really easy to understand at least for me and from the short snippet you provided you seem to have a consistent coding style and be using whitespaces in the right amount and at right places - that counts a lot for clean code (if you don't believe me just take a look at some Perl snippets).
I would only like to point out three things:
rSum
is not a awful name for a property it isn't quite clear what values does it hold, perhaps you could find a more descriptive name for that property?Your second "real" example could be cleaner and faster if you use the right functions:
$this->rSum = array_flip(explode(",", $this->options["rSum"]));
EDIT: I just noticed the code I provided above is not quite what you're doing (the
0
was not processed by my brain), here is another working alternative:There seems to be a lot of people here that don't like one-liners I however, believe that the the above code is clear, efficient and descriptive - but that may be just me... =)