Rasmus Schultz, web开发者,从1998开始用过很多种语言、框架和数据库。
因为这是真的。
现在,我知道因为我不再为PHP辩护而不再受欢迎,请大家见谅。
首先,PHP的问题清单太长了,都不好回复在这里。建议去读这篇著名的文章,里面很清楚的列举了PHP的主要问题。
现在,在大家准备对我口诛笔伐之前我先声明:我每天都用PHP,而且用了很多年了 —— 我并不恨PHP。事实上,我喜欢它:-)
你看,虽然PHP确实是设计得最烂的语言,但是它确实很有用。
你之所以听到一些开发者说它是“设计得最烂的语言”,通常这些人都使用过别的语言,不是因为PHP设计的烂,是它根本就没有设计。
PHP不是设计出来的 —— 是一步步发展出来的。
换句话说,根本没有一个设计团队像模像样的坐到一起讨论语言的开发计划和设计方案 —— 最初是一个家伙一时兴起搞了个原型出来,然后另外的家伙往上面打补丁增加新特性,就这么个状态过了好几年。所以你可以看到PHP里面有很多互相矛盾的特征,因为它的开发社区长期处于无政府状态。没有方向,没有计划,没有讨论,没有资金,没有任何确保一致性的东西。
这就导致PHP充斥了不一致和不可预知的特这 —— 这是程序员最讨厌的。
PHP同时还被另一个烂设计所禁锢 —— 作为一种语言,你不能改变一些根本的实现方式。我们只能不断增加新特性,却不能修改或删除一些固有的特性或实现方式。那样会导致现有的PHP代码无法运行,那就不再是PHP了,那样会失去活跃的社区,而大量的开源代码库也会消失。
因此,PHP能够成长,但不能改变。
根据我的经验,那些称赞PHP的人大多分不清语言和平台。他们说PHP好因为PHP能连数据库,能操作图片,有优秀的包管理工具(Composer)和很多有用的开源代码库、框架、模板引擎、ORM,以及一个充满活力的社区。然而,所有这些说的都不是语言本身。
人们之所以分不清语言本身和语言平台可能是因为对他们来说这就是一回事儿 —— 都是说的“PHP”。不过事实上他们喜欢的不是PHP语言,而是PHP平台。比如:用C编写二级制的扩展,这是语言特性;活跃的社区,这是平台的特征。
攻击PHP的人则是着眼于语言本身 —— 语法、范式、操作符、结构体、变量概念和作用域、类型系统等等。所有这些方面的吐槽也通常会发生在其他语言身上。
综上所述,我的结论是 —— PHP是有用的。开发Web系统所需要的东西它基本全有。我意思是,事实摆在这呢。人们使用PHP,那PHP就是有用的。
它可能不具备其他语言那些我们喜欢的高级特性,但它确实让我们完成了目标 —— 而且通常都比别的语言更直接、更快速。这当然要感谢千锤百炼的运行时、可用的各种代码库、工具和社区 —— 当然还有语言本身。
PHP是丑陋的。他不是我最喜欢的编程语言,但是它是我最趁手的工具:-)
Rasmus Schultz, web-developer since 1998 using many languages, frameworks and databases
Because it’s true.
Now, I know I’m going to be less than popular for not jumping to PHP’s defense, but bear with me.
First off, the list of problems with the PHP language is far too long to even post in a reply here - go read this infamous article, which neatly sums up most of the major issues.
Now, before everyone starts spitting flames at this reply, let me say that I work with PHP every day, and have for many years - and I’m not a hater. In fact, I enjoy it :-)
You see, while PHP is definitely one of the worst languages ever created, it’s also tremendously useful.
The reason you will hear comments like “worst language ever”, and usually from developers who are have real working experience with other languages, is because the language itself suffers, not from poor design, but from a complete lack of design.
PHP was not designed - it was grown.
In other words, you didn’t have a team of engineers sit down and plan, discuss and design the language - you had one guy whip up a prototype, another guy patch on another feature, and so on, for many years. So PHP was influenced by many conflicting points of view, and historically had very little government, no real direction, planning, coordination, funding, or any sort of consistent influences.
This leads to a language that is filled with inconsistencies and unpredictable behavior - the sort of things that people who love programming tend to hate.
PHP is also stuck in a kind of spiral of bad design - as a language, it’s unable to really change in any substantial way. We can add new features - but we can’t really substantially change the language or remove existing features, because existing PHP code would no longer work, the language wouldn’t really be PHP anymore, and the value proposal of a vibrant community, and a vast library of open-source code, would disappear.
Thus, PHP can grow, but it can’t really change.
In my experience, people who sing the praises of PHP tend to have trouble separating discussions about the language itself from discussions about the actual platform. They will say that PHP is great because it has database connectivity, image manipulation, a nice package manager (Composer) and lots of useful open-source libraries, frameworks, template engines, object/relational-mappers and a vibrant community; all things that have nothing to do with the language.
It may be difficult for someone to separate discussions about the language from discussions about the platform because they’re one and the same - they’re both referred to as just “PHP”. But actually, most of the things that people appreciate about PHP are not features of the language: they’re features of the platform, e.g. binary run-time modules written in C, like the language itself - or “features” of the community around it.
Developers who attack PHP tend to come at the language itself - the syntax, paradigms, operators, constructs, variable concepts and scoping rules, the type system, etc. - all of which are so filled with gotchas and wtfs that almost any other language is going to shine in comparison.
With all that said, the opposite side of the coin is simply this: PHP is useful. It has basically everything anyone wants from a web development platform. I mean, the proof is in the pudding, right? People use PHP, so by definition it is useful.
It may not have many of the things that some of us love (and envy) about other languages, but it lets us get down to objectives - often more directly and much faster than with other platforms. This is thanks to things like solid, battle-tested run-times and available libraries, tools and (!) community - and in spite of the language itself.
PHP is ugly. It’s not my favorite language, but it is my favorite thing to work with :-)