带有命名空间的 php 程序与 oop
据我了解,OOP 与 PHP 中的过程式编程相比的最大优点是函数名称的分离(某种命名空间)。
那么现在,当我们从 5.3 版本开始就有命名空间时,您怎么看?对于大多数情况(中小型网站),当我们需要快速且再次结构化的代码时,使用命名空间 + 过程编程比定义和编写具有显着优势面向对象编程。
优点:
- 再次构建
- 更快的代码/开发,
- 我们可以在以“_”开头的命名空间中定义类似私有函数的东西,知道我们不需要使用它们
- 等。
代码示例:
namespace User;
function setPassword ($user_id) {
$pass = _generatePassword();
$sql = 'UPDATE `users` SET `password` = '.escape($pass).' WHERE `user_id` = '.escape($user_id);
$result = mysql_query($sql);
if (mysql_affected_rows() == 1) return $sql;
else return $sql;
}
function _generatePassword () {
$char = '0123456789abcdefghijklmnopqrstuvwxyz';
$str = '';
for ($i = 1; $i <= 6; $i++) {
$str .= $char[mt_rand(0, strlen($char))];
}
return $str;
}
用法:
$user_id = 5;
User\setPassword($user_id);
我正在征求意见。我知道这只是开发人员的风格,但也许我错过了一些东西。
附言。对于大多数情况(中小型网站) - 我的意思是当您为客户制作网站时,这些网站大多是一次性开发,从长远来看,需要进行一些功能改进。
The biggest advantage of the OOP vs procedural programming in PHP to my understanding is the sort of separation of the function names (sort of namespace).
So now when we have namespace since version 5.3, what do you think - For most cases (small to mid websites), when we need fast and again structured code, do the use of namespace + prodecural programming gains signifficant advantage over defining and writing in OOP.
Advantages:
- structured
- faster code/development
- again we can define something like private functions within the namespace starting with "_" knowing that we don't need to use them
- etc..
Code example:
namespace User;
function setPassword ($user_id) {
$pass = _generatePassword();
$sql = 'UPDATE `users` SET `password` = '.escape($pass).' WHERE `user_id` = '.escape($user_id);
$result = mysql_query($sql);
if (mysql_affected_rows() == 1) return $sql;
else return $sql;
}
function _generatePassword () {
$char = '0123456789abcdefghijklmnopqrstuvwxyz';
$str = '';
for ($i = 1; $i <= 6; $i++) {
$str .= $char[mt_rand(0, strlen($char))];
}
return $str;
}
Usage:
$user_id = 5;
User\setPassword($user_id);
I am asking for opinion. I know that it is just to the developers style, but maybe I am missing something.
PS. For most cases (small to mid websites) - I mean when you do websites for clients which are mostly 1 time development, and a little feature improvements in the long run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对 OOP 的思考方式是错误的。如果您试图将 OOP 与过程命名空间仅作为组织代码和函数调用的两种不同方式进行比较,那么命名空间肯定会显得更有效。
OOP 的优点不在于组织一个充满功能的对象。这只是将 OOP 类视为充满函数的大“utils”类。 OOP 的优点不是组织性的。这是一种完全不同的构建程序的方式,它使您将代码分解成更小的、离散的实体。我在所有 PHP 程序中都使用 OOP,即使是小项目也是如此。
当我做任何访问数据库的项目时(这几乎是现在的一切),OOP 的优势对我来说变得最为明显。我创建小类来对每个数据库表进行建模,然后将这些表中的信息作为对象进行访问。我在所有项目中使用了一些基类,这些基类定义了如何将表映射到对象,因此我不再重新输入或粘贴 mysql 命令。我只使用这些对象,它们继承了从数据库插入、更新和删除所需的所有功能。
在代码中(特别是如果您使用具有代码完成功能的 PHP ide)在代码中看到这一点肯定更有用:
比这更有用:
差异可能不会立即明显。这两个示例都是用于打印表列的一行代码。但第二个例子要求我知道我脑子里的列名。第一个示例将列名称作为属性嵌入到类中。我的代码检查器知道所有属性,因此当我编码时,它会在我键入时显示所有属性的列表。
维护课程比您想象的要容易。根据您选择的对象框架,有一些脚本可以从表生成类并使其保持最新。而且我发现,让我的对象类保持最新状态比让数据库更改破坏代码要少得多,因为列名发生了更改,然后我必须在许多地方更新这些列引用。是的,有搜索和替换,但是您是否看到为列更改更新一个文件比更新对 $row['some_column'] 的每个引用的优势?
我希望这有助于回答您的问题。
You are thinking of OOP the wrong way. If you are trying to compare OOP with procedural namespaces merely as two different ways of organizing your code and function calls then certainly namespaces will seem more efficient.
The advantage of OOP isn't in organizing an object full of functions. That just treats OOP classes as big "utils" classes full of functions. The advantage of OOP isn't organizational. It is an entirely different way of building your programs that causes you to break your code into smaller, discreet entities. I use OOP in all of my PHP programs, even for small projects.
The advantage of OOP becomes most clear for me when doing any project that accesses a database (which is pretty well everything nowdays). I create small classes to model each database table and then access the information in these tables as objects. I have some base classes I use in all my projects that define how to map tables to objects so I don't retype or paste mysql commands any more. I just use the objects and they inherit all the needed functionality for inserting, updating and deleting from the database.
It sure is far more useful in code (especially if you use a PHP ide that has code completion) to see this in code:
Than this:
The difference might not be obvious right away. Both examples are a single line of code to print a table column. But the second example requires that I know the column names in my head. The first example has the column names embedded in the classes as properties. My code inspector knows all the properties so when I code it presents a list of all the properties as I type.
Maintaining the classes is easier than you think. Depending on the object framework you choose there are scripts to generate classes from tables and keep them up to date. And I find it FAR less error and bug prone to keep my object classes up to date myself than to have database changes break code because column names changed and then I have to update those column references in dozens of places. Yes, there is search and replace, but do you see the advantage of updating one file for your column change than updating every reference to $row['some_column']?
I hope this helps answer your question.
我认为这是一个有效的问题。对于 OOP 风格的编程,随着问题空间的增长和代码库的增加,往往会引入大量的开销。可以公平地说,对于小型项目,使用 OOP、函数式或过程式之间没有任何真正的区别,但此后情况并非如此。
OOP 编程原则所吹捧的优点之一(尽管不是唯一的优点)是命名空间给您带来的好处。此外,在很多情况下,强制使用类会引入另一个级别的更紧密的耦合和一些依赖性。这里建议只使用命名空间来编写过程代码,这将允许以更自然的方式重用函数。
在一般情况下很难更具体,并且原始问题中提供的示例没有揭示出于上述原因避免使用类的一些潜在好处。
一些不使用 OOP 风格的论点与支持和反对函数式编程语言的论点更具可比性。这里要添加的一个有趣的例子是看看由 google 的人编写的相对较新的 go 语言,它更进一步,允许使用自己的命名空间中的包来独立于结构或接口定义函数。
如果谷歌的人看到这里提出的方法有一些优点,那么对于我来说,考虑一下这似乎并不是一件坏事,而且不仅仅是值得深思的事情。
I think it is a valid question. With OOP style programming there tends to be a lot of overhead introduced as the problem space grows and the code base increases. It's fair to say that for small sized projects, there isn't any real difference between using OOP, functional or procedural, but this is not the case thereafter.
One of the advantages, although not the only one, touted by OOP programming doctrine is the benefit that namespacing gives you. Also, forcing the use of classes introduces another level of closer coupling and some dependancies in a lot of cases. What is proposed here is to write procedural code using only namespaces, which will allow reuse of functions in a much more natural way.
It's hard to be more concrete in the general case, and the examples presented in the original question don't reveal some of the potential benefit of avoiding the use of classes for the reasons mentioned.
Some of the arguments for not using an OOP style are more comparable with the arguments for and against functional programming languages. An interesting example to add here would be to look at the relatively new go language, written by the guys at google, which goes one step further and allows functions to be defined separate from structs or interfaces, using packages in their own namespace.
If the guys at google see some merit in the approach presented here, for my money, it doesn't seem such a bad thing to consider, and more than food for thought.