I have nearly two decades of experience in programming, and use a variety of programming languages for development. In a lot of work and is now doing the job I’ve done, I will be very pleased to PHP as the core programming language. From the first use PHP to work on, I heard about PHP’s various complaints, but at the same time I have seen the power of PHP.
This article was originally published in PHPArch site, authorized by the original author Chris Tankersley, InfoQ Chinese station translate and share.
PHP at least an interesting programming language. The language and use it to build programs generally fall into two design philosophy. Here, I am talking about is not the software development life cycle, such as a waterfall or agile, but about what kind of software should be the basic idea. These ideas are called “the right way” (The Right Way) and “worse is better” (Worse is better).
PHP is a rather odd programming language. When people complain about the language “very groove cake”, they were not wrong. This language does have a lot of bad places. Put in the past, there is more bad problem this language. Laugh PHP Bowen “comprehensive analysis PHP slot cake design” (PHP: a fractal of bad design) does have some valid point, even if these ideas published nine years ago, is outdated.
However, at the same time, but developers can use PHP to create a structure to “correct” software, and the introduction of philosophy is considered good practice from other languages. Like Symfony framework Laminas and best practices on the use of object-oriented programming, so developers can write code with the correct structure of these frameworks.
PHP is how to do that? This is because PHP is the worst programming language.
1991 years, Richard P. Gabriel published an article “Lisp: Good news, bad news, how to win big” (Lisp: Good News, Bad News, How to Win Big). Argument of this article is, in terms of software design and life, “worse is better” philosophy would be a better choice. The reason why he came to this conclusion because he realized that there were two different programming genres, he will be named respectively as “MIT / Stanford style” (MIT / Standford Style), or the “right mode “, and” New style “(New Jersey style) or” worse is better. “
These two objectives similar philosophy, but is different in key areas. Both styles are focused on four key areas of philosophy: simplicity (Simplicity), correctness (Correctness), consistency (Consistency) and integrity (Completeness).
MIT style is described:
- Simplicity: the design must be simple, regardless of its implementation or interfaces, all must be simple. In contrast, keep it simple so that the interface is more important.
- Correctness: all can be observed in all aspects of design must be correct. Do not stop dreaming about a correct design.
- Consistency: the design must not be inconsistent. To ensure consistency, you can sacrifice a little simplicity and integrity. Consistency and accuracy are equally important.
- Integrity: the design must encompass as many important situations. All cases in line with expectations must be covered. Priority should be higher than the integrity of simplicity.
With regard to New Jersey style, Gabriel said it would its objectives be defined as:
- Simplicity: the design must be simple, regardless of its implementation or interfaces, all must be simple. And by contrast, keep it simple so that realization is more important. Simplicity is the most important, other features are not as simple to keep more important.
- Correctness: In all aspects can be observed, the design must be correct. But for simplicity while slightly sacrificing accuracy.
- Consistency: the design must not be too inconsistent. In some cases, simply in order to ensure consistency can be sacrificed. If an unusual case design is introduced, it will lead to the realization complicated or inconsistent, then do not consider this situation.
- Integrity: the design must encompass as many important situations. All cases in line with expectations must be covered. You may compromise the integrity of any other features. In fact, once a threat to the realization of simplicity, integrity must be sacrificed. If in order to keep it simple, Consistency can be sacrificed to achieve integrity; in particular, the consistency of the interface.
The key to this debate is to explain why “worse is better” with LISP and C as an example. For LISP programmer for Gabriel, LISP is a better language than C, and C as fast speed, and Common LISP design, development and standardization have spent a lot of years. Defined specification of the language absorbed the essence of all the different LISP, and modern development environment is best for LISP developers.
LISP is the right way
LISP represents the software development of the “right way.” LISP is easy to interact with, you can interact with it in various ways. I want to call from Fortran, LISP? You can call the LISP and the data is passed from Fortran, and vice versa. When using legacy code, you can happily use all the modern “luxury” feature of LISP.
LISP has a consistent design, thanks to its specification. If you look at modern languages such as Python, norms have a great role in providing multiple back-end compiler, and in the same way they are interpreted or compiled code. These tools are first class, in 1991 LISP has all the comforts we still enjoy today, such as step debugging, data checking and fancy editor.
As a language, LISP is complete. It has advanced object-oriented programming layer, multiple inheritance, class objects and functions and types. LISP seems to be the hearts of developers who want programming language.
1991 years, LISP programming language may be so in the best condition ever. This technical correctness has not been confirmed by actual use. LISP developers are recession. Over the years of negative news and wrong positioning hampered external reputation of LISP. People no longer be regarded as a way to deliver software to end users.
In terms of development, LISP often represents many of the “large-scale pre-designed” (Big Design Up Front, BDUF) the same ideals. If you’ve ever used the Waterfall model (Waterfall Model) This design method, you will find some problems. “The right way” places great emphasis on consistency, accuracy, and to ensure consideration of all the issues can think of.
LISP itself is not a single language, but a language family. Although the Common LISP is designed as a standard, but the implementation of LISP itself is done according to various work needs exist. An article Lockless Inc website pointed out that this “fragmentation” is one of the determinants of LISP ultimate failure. Although LISP adhere to software design “the right way”, but this fragmentation lead to code maintenance and portability have been affected.
C and Unix are the wrong way
At the same time, due to the emergence of Unix, C language gradually become the preferred method of software development. C language is designed for Unix, and Unix is written in C language design. Its developers and MIT LISP and its author has a different design positions.
In 1972, C was designed to be a simple language. By the year 1991, it has undergone some changes, but the basic principles of C language has not changed. Some features in order to meet the needs of developers and Unix are added. Because the language is very simple, so writing compilers and program very easy. Although this language does not prevent you from complex programming, but compared with LISP, C language programmer is estimated that only 50-80% of the required characteristics.
However, C language there is a strong portability. Common LISP with respect to the hardware and software environment, it can also run on low power hardware. This factor makes it possible to compile and run the software in a wider range of machines. C language and Unix are easy to use, Gabriel believes Unix and C language will be as popular as viruses.
Dennis Ritchie in the design and construction process of Unix, C language has been developed. Because Bell Labs (Bell Labs) are not allowed to officially enter the field of computers, so Unix can be easily distributed to a variety of different users. These help repair Unix users to meet their own needs. Dennis Ritchie can be based on the needs of these patches together, without prior consideration of these needs.
PHP most slot cake
Therefore, the “worse is better” software will be accepted first, followed by the less it will make user expectations, third, the software will be improved constantly, until the proximity of the “right approach”.
— Richard Gabrie
In this revelation a few years later, Rasmus Lerdorf began to study Personal Home Page / Forms Interpreter, which is what we now know as PHP. The birth of PHP / FI is because Lerdorf need to maintain his home, and interact with forms and databases. PHP / FI was not even as a practical programming language design, but functions as a layer on top of the script and the C language design.
PHP very simple
The design must be simple, whether it is the realization of the interface.
PHP use the underlying C language, we have said before, this part is “the worst.” However, it also brought some advantages, most importantly, the simpler the underlying language can make it easier to expand. Although Hack / HHVM uses more C ++ way, but PHP itself is still C language.
in just a few hours after studying the internal structure of the language. Elizabeth Smith published a wonderful speech about PHP extension, which describes a lot about the inner workings of PHP. The language itself borrowed from other C-style language, is not only easy to read, and can be converted with other C-style language with each other.
PHP most of the interface, or standard library is very simple, because most of the core functions are simply packaging a variety of C language library, then almost intact made public. Although doing so would lead to some inconsistencies in the interface, but it provides a familiar environment for developers from C or C ++.
PHP language is very focused on Web development. The HTTP conceptual extracted and found similar language is usually very simple concept. A request header want to know it? get_headers () will be able to meet you. A request for information as to read $ _GET and $ _POST global variable as simple.
PHP developers to maintain a simple interface and keep it simple internal structure as much as possible.
PHP (almost) is right
In all aspects can be observed, the design must be correct. But for simplicity while slightly sacrificing accuracy.
Here, PHP tend to choose “simple” rather than correct. Before HHVM appearance, the appearance and features of the language has not been standardized. Zend interpreter itself is the norm, and behavior of this language is always the “right” (not including the actual error). To use something else instead of the PHP engine, it is necessary to implement all existing engines.
许多核心函数的 LAX 函数参数和返回类型都使得系统的工作更容易。像 strpos() 这样的函数返回值可以是整型数或布尔值，相对于严格设计成返回整型数或抛出异常的方法，处理要稍微容易一些。
Development of the PHP language, almost all of the new features are based on the developers need, rather than “because it is wrong it must be repaired,” the idea seriously. Pay more attention to those types and strict exception error is a more correct way of doing things. However, there are some things, such as a short arrow function (arrow function), and enumerated attributes, is the developer wants to simplify things code.
PHP do not need consistency
The design must not be too inconsistent. In some cases, in order to keep it simple Consistency can be sacrificed.
I’m not even going to pretend that PHP is the same, but its consistency is sufficient. When it comes to the array and string functions, people may complain needle / haystack parameter order. However, in general, the array function is the same, and string functions are also the same. Remain consistent with the underlying C library is much easier than keeping in the language.
PHP in other ways consistent enough. As I mentioned in strpos () in, PHP for functions that encounter errors tend to be fairly consistent returns FALSE. This is not necessarily true, but it is consistent. Underlined and function names are usually no match underlined its foundation library.
For simplicity, PHP language at the expense of consistency, but even without this specification, it is still trying to be consistent in where it makes sense.
The design must be covered as many important situations.
Whenever, in the greatest need for PHP design tasks: When writing Web applications, PHP is complete. PHP was never designed as a language can be applied to all the problems of the world of programming. Nevertheless, its simplicity is that it can be used for applications other than the Web. PHP initial aim is to provide the basic functionality for Web programming, a trend that continues to this day.
Modify the core language is usually driven by the needs of developers. Propose amendments to the whole community, then vote via the community decide the new features is rejected, accepted or changed. Many innovations are derived from the language needs to quickly complete the work. Even if we absorbed the functions of other languages, but also because it makes our development easy, but rarely done because other language “is more correct.”
Today, you can use PHP to develop Web applications. After five years, you can still use PHP to develop Web applications, but will add some new features. However, the integrity of the language itself has met the required today. If in the future there is a need, we can change the language at any time or to add new features to it.
Worse is better?
Gabriel acknowledged that “worse is better” philosophy refers to the design looks very bad, and perhaps should not be the better choice. The only problem is that when he look at these two philosophies, compared with the MIT / “right way” design philosophy, “worse is better” is still ultimately more flexible options, “better survival characteristics. ” If we look at PHP, we can confirm that “worse is better” this view.
Over the years, Gabriel admitted that he was wavering between what a better way. PHP community has been arguing that we should be doing things right, or simply continue to work. We have like Laminas framework, the classic way of building computer science library, and then we have like Laravel framework, attention and speed of the developer’s experience. Both PHP itself both.
The next time you hear someone criticize PHP time, just let him go spray. This language is really bad. But in many ways, longevity and widespread use of PHP proves this fact: with the “right way” of doing things does not always do things better than “the worst” way. When someone Tucao framework you’re using, you have to understand that in the long run it does not matter. Select one you think suitable for their own design philosophy and embrace it: worse may actually be better.
Chris Tankersley has many roles: husband, father, author, speaker, and host podcasts PHP developers. Chris uses many different frameworks and programming language in 12-year career, but most of his day in the use of PHP and Python. He is the “author Docker for Developers (no Chinese version) and in cooperation with companies and developers to integrate into their containers workflow.