`

php缺点

    博客分类:
  • php
阅读更多

http://www.bitstorm.org/edwin/en/php/

What I don't like about PHP

Edwin Martin <edwin@bitstorm.org >.

Published: August 6th, 2004; Last update: November 26th, 2009.

I have been developing in PHP since 2001. PHP is very easy to program in. But PHP also has some serious flaws.

Below I give some reasons why you have to do some serious thinking before implementing a large scale web application in PHP.

1. Bad recursion

Recursion is the mechanism in which a function calls itself. This is a powerful feature which can make something complex something simple. An example of a function using recursion is quicksort . Unfortunately, PHP is not good at recursion. Zeev, one or the developers of PHP, says this: "PHP 4.0 (Zend) uses the stack for intensive data, rather than using the heap. That means that its tolerance recursive functions is significantly lower than that of other languages." See bug 1901 . This is a poor excuse. Every programming language should provide good recursion support.

2. Many PHP-modules are not thread safe

A couple of years ago, Apache released version 2.0 of its web server. This version supports multithreaded mode, in which one piece of software can run multiple times simultaneously. The makers of PHP say that the core of PHP is thread safe, only the non-core modules often aren't. But nine out of ten times, you do want to use such a module in your PHP-script, making your script unsuitable for Apache's multithreaded mode. And according to some, even the core of PHP isn't thread safe . So it is no surprise the PHP Group doesn't recommend running PHP on Apache 2 in multithreaded mode . The bad multithreaded mode support of PHP is often seen as the reason Apache 2 still didn't catch on.

Read the discussion about this on Slashdot: Sites Rejecting Apache 2? .

3. PHP is crippled for commercial reasons

The performance of PHP can be increased to 500% by using caching [benchmarks ]. So why is caching not build into PHP? Because Zend, the maker of PHP is selling its own Zend Accelerator and of course, they don't want to cannibalize on there own commercial products.

But there is an alternative: APC .

4. No namespaces

Suppose someone creates a PHP-module that can read files. One of the functions in this module is called read. And someone else's module can read web pages and also contains a function read. Then it is impossible to use these modules together because PHP will not know which read function you want.

An easy solution to this is namespaces. It was a suggested feature for PHP 5, but unfortunately it didn't make it . Now, without namespaces, every function has to be prefixed with the module name, to prevent name collisions. This leads to terrible long function names like xsl_xsltprocessor_transform_to_xml which makes code harder to write and read.

Finally, since version 5.3 PHP has namespaces. Unfortunately, as as level seperator, it uses the backslash (\) character, normally used to escape other characters.

5. Non-standard date format characters

Many programmers are familiar with the date format characters known from UNIX and the C programming language. Other programming languages adopted this standard, but strangely enough, PHP has its own, completely incompatible date format characters. While in C, "%j" stands for the day of the year, in PHP this stands for the day of the month. To make things even more confusing: the function strftime and the date_format of Smarty, a PHP templating engine, do use the C/UNIX format characters.

6. Confusing licenses

You might think PHP is free and all PHP-modules in the manual are free too. Wrong. For example, if you want to generate a PDF-file from PHP you will find two modules in the manual: PDF or ClibPDF . But both come with commercial licenses. So for every module you use, you have to make sure you agree with its license.

7. Inconsequent function naming convention

Some function names consist of more than one word. There are three conventions for combining these words:

  1. Glued together: getnumberoffiles
  2. Separated with underscores: get_number_of_files
  3. Camel case: getNumberOfFiles

Most Languages choose one of these variants. PHP uses all of them.

For example, it you want to convert special characters to HTML entities, you use the function htmlentities (Words glued together). If you want to do the opposite, you use its little brother function html_entity_decode . For some reason the words are now separated by underscores. Why is this bad? You know there is a function named strpad. Or was it str_pad? Every time you have to look up what the notation is or wait for an error to occur. Functions are case insensitive, so for PHP there is no difference between rawurldecode and RawUrlDecode . This is bad too, because both are used and because they look different, they will confuse the reader.

8. Magic quotes hell

Magic quotes prevents PHP-scripts from SQL injection attacks. That's okay. But for some reason, you can turn this off in the php.ini configuration file. So if you want to write a portable script, you always have to check whether magic quotes is turned on or off. So a "feature" to make programming easier, actually makes it more complex.

9. Framework seldom used

A website wihout a framework which grows will eventually become a maintanance nightmare. A framework can make a lot of work easier. The most popular model for a framework is the MVC-model, in which layout, business logic and interaction with the database are seperated.

Many PHP web sites don't use the MVC-model . They don't even use a framework. Although some PHP frameworks do exist and you can also write your own, articles or manuals about PHP don't say a word about frameworks. While JSP-developers use frameworks like Struts and ASP-developers use .Net, it looks like the concept of a framework is largely unknown by PHP developers.

Update: Zend, the developer of PHP, created a framework Zend PHP Framework . Other frameworks, like CakePHP , CodeIgniter and Symfony are also getting popular. Only the Zend framework is really object-oriented.

10. No Unicode

With Unicode it is possible to use every language in the world, for example Chinese, Arabic and Hebrew. While every other serious programming language supports Unicode for many years, PHP still has a very hard time dealing with this. Unicode is planned for PHP 6 , so we still have to wait a long time before PHP supports this trivial feature.

11. Slow

You think Java is slow? PHP is much slower ! See this Computer Language Shootout . So how can PHP be used on all these popular websites with lots of visitors? Because of caching. These sites use memcached and APC to get performance. It doesn't proof anything about PHP, only that it's cachable.

Even Rasmus Lerdorf, the creator of PHP, admits this. In this interview from SitePoint , Rasmus is asked about how to make PHP fast. He answeres "Well, you can't".

12. Shared Nothing

A slow interpreter wouldn't be a big problem if you could solve it by adding a couple of extra servers. How easy is that? PHP doesn't have threads and you can't share data between different PHP-processes. PHP-supporters call this the "Shared Nothing-architecture". Very smart to sell a shortcoming as an advantage. Just like any other system, you have to share data and in PHP, you do it through the database. Is moving this problem to the (already slow) database really the right solution? I don't think so.

A simple PHP script can become less than 100kB. But a single page in a serious system or in a CMS can easily become 15MB (yes: megabytes). For every request, this memory has to be initialized, which takes time. It also limits the number of concurrent connections. If you have 2GB available for PHP, you can only have 2000/15 is 133 simultaneous connections. Not much, really.

In the SitePoint-interview mentioned above, Rasmus also claims PHP doesn't scale.

Facebook is a nice case to proof this. Facebook is probably the biggest PHP-site. They have 2.3 billion visits per month. They also use 30 thousand servers. That's a lot. If you do some calculations, you'll find out, on average, one server handles only 104 visits per hour. That's really poor performance.

Conclusion

PHP has two advantages: it's very easy and it's widely supported by webhosting companies. But that doesn't make it a good language.

For very small projects, it can be a nice programming language. But for larger and more complex projects, PHP can show its weakness. When you search long enough, you'll find solutions (work arounds) to some of the mentioned problems. So when a solution is known, why isn't it fixed? And why are the fixes not mentioned in the manual?

It's good that an open source language is very populair. Unfortunately, it's not a great language. I hope all problems will be solved once (in PHP 6?) and we will have an open source language that's open source and good.

Until then, when you start a project larger than five scripted pages, you might also consider C#/ASP.Net or Java/JSP as a better solution. And if you want really good (distributed) performance, you can look at some initiatives which have been popping up the last couple of years. Just to mention a couple of them: Nginx , Couch DB and Node.js . They all blow existing systems away.

After I wrote this page, some people informed me about other, similar pages:

分享到:
评论

相关推荐

    php在线查杀扫描webshell后门 对接WEBDIR+ Api

    缺点:每次查杀都需要上传文件到WebDir 并等待返回结果 速度比较慢 需要耐心等待 优点:Api 免费 并且不限制上传文件数量。 WEBDIR+采用先进的动态监测技术,结合多种引擎零规则查杀,低误报、高查杀率。 Html Gui...

    浅谈php的优缺点

    本文给大家简单总结了php的10点优点和3个缺点,都是个人对于php开发的一些体悟和总结,有需要的小伙伴可以参考下。

    优秀的PHP程序员至少应该了解PHP代码的优缺点

    优秀的PHP程序员至少应该了解PHP代码的优缺点

    从PHP看面向对象和面向过程的优缺点评比

    学习PHP不可不知道的知识,从PHP来看面向对象和面向过程的优缺点……

    谈asp,php,jsp的优缺点.docx

    谈asp,php,jsp的优缺点.docx

    java和C#和PHP和各种数据库优缺点.docx

    java和C#和PHP和各种数据库优缺点.docx

    主流PHP框架的优缺点对比分析

    主要介绍了几款今年比较热门的主流PHP框架的优缺点对比分析,非常的简单实用,有需要的小伙伴参考下。

    浅谈php常用的7大框架的优缺点

    一直以来,phper讨论最多的就是php各种框架的优缺点,网上的资料也是比较零散,现把几款主流的框架收集汇总一下,其中本人只是用过Yii2、Laravel、Yaf、Thinkphp这四种框架,因此大部分对各种框架的评价皆来自与网上...

    php7.3.8手册(官网).zip

    在现实生活中,这是一个巨大的缺点,因为程序员无法防止意外的返回类型并在其他情况下生成异常。 幸运的是,PHP 7允许程序员根据期望的返回值声明函数的返回类型。这肯定会使代码健壮和准确。有四种不同的返回类型...

    Zend_API:深入_PHP_内核

    由于受到PHP语言本身的限制,同时还可能不得不把庞大的库文件包含到每个脚本当中,因此,某些新功能并不是总能被顺利实现,所以我们必须另外寻找一些方法来克服 PHP 的这些缺点。 了解到了这一点,我们就应该接触...

    谈谈关于php的优点与缺点

    本篇文章小编与大家一起谈谈关于php的优点与缺点问题,有需要的朋友可以参考一下

    PHP100视频教程 27:PHP模板引擎Smarty入门使用.rar

    软件介绍 1、Smarty介绍及应用的优缺点  Smarty是一个使用PHP写出来的PHP模板引擎,目的是要使用PHP程序同美工分离,使的程序员改变程序的逻辑内容时不会影响到美工的页面设计,美工重新修改页面时不会影响到程序的...

    phpredis-2.1.3.zip + Redis客户端介绍+ php调用Redis教程 +Redis相关方法说明

     这是一个二进制版本的PHP客户端 按照的说法 效率要比Predis高 这个版本支持作为Session的Handler 这个扩展的有点在于无需加载 任何外部文件 使用比较方便 缺点在于难于扩展 一般的PHP程序员无法对其做出扩展 考虑...

    fackbook 将php转c++ 代码

    随着 Ajax技术的广泛采用,加上SNS对动态要求较高,这些缺点更显得突出。对于每月超过4000亿次PV的Facebook来说,如何实现扩展,尤其具有挑战性。 常见的办法是直接用C++重写PHP应用中比较复杂的部分,作为PHP扩展...

    谈asp,php,jsp的优缺点.pdf

    。。。

    PHP实现随机显示目录下的图片

    可是用JS做的话,有以下缺点: 1.万一浏览器禁用了JS的话就失效了,而且写代码是需要考虑兼容性。 2.维护比较麻烦,图片的位置都存放在数组里。 于是我提议用PHP处理,可是我和她对PHP都是半桶水的,一时之间也...

Global site tag (gtag.js) - Google Analytics