Saturday, January 12, 2013

Top 10 Wrong Ideas About PHP

divtag Online Web Tutorials
Top 10 Wrong Ideas About PHP

  1. PHP is not a compiled language (it is interpreted)
  2. PHP cannot do X (access memory, control hardware devices, or some unusual purpose)
  3. PHP cannot do something that can be done in language X
  4. PHP is only for Web development
  5. PHP is controlled by only one company (Zend)
  6. PHP documentation is bad or insufficient
  7. PHP projects are not reusable because they are not Object Oriented
  8. PHP is worse than Ruby On Rails, Python Django, X language Framework
  9. PHP is not good for high performance scalable Web sites or applications
  10. PHP developers are cheaper because they are not qualified
  11. PHP is not a compiled language (it is interpreted)

  1. compiled language is one that needs to convert source code in that language into a sort of machine code before it can be executed.
  2. An interpreted language is one that allows its code to be executed directly from the source text without a compilation step (convert source text into executable machine code).
  3. PHP is not an interpreted language since PHP 4, which was launched in the year 2000.
    1. When a PHP script is executed, first the PHP source code is compiled by the Zend engine into machine code data named Zend opcodes. These opcodes are stored in the RAM. Then those opcodes are executed to actually run the script.
    2. Diagram of the compilation of PHP source code into Zend Engine opcodes
    3. So PHP is really a compiled language just like Java, C# and others. Otherwise it would be rather slow.
  4. Usually the compiled PHP machine code (Zend opcodes) are not saved to files because it is not necessary. But if that is important to you, there are extensions that can output compiled PHP code to files.
  5. By default, if you run the same script again, PHP source code needs to be recompiled into the RAM every time it is executed. However, there several opcode caching extensions that can save the compiled PHP opcodes to shared memory, so next time a PHP script executed for a different Web server request, the original source no longer needs to be recompiled. It is just loaded from shared memory, thus saving a lot of processing time.
  6. The use of an opcode cache extension is thoroughly recommended for performance reasons, especially on busy sites. There are several free opcode caching extensions that you can use.
  7. So, as you may understand by now, the PHP code execution engine is quite sophisticated these days. But if you need something even more sophisticated, there is the HipHop for PHP compiler. This is an Open Source project developed by Facebook to take the PHP performance to the extreme. It compiles PHP source code into C++, which is then compiled into native machine code into a single Web server binary or a standalone executable program. Facebook uses it to run most of their site.
  8. There are also other PHP compiler projects that convert PHP code into Java bytecodes or .NET assemblies. More details can be found on the PHP compiler performance article already mentioned above.

PHP cannot do X (access memory, control hardware devices, or some unusual purpose)

  1. PHP is an extensible language. If you need something that the main PHP distribution does not implement, you can create PHP extensions, usually by writing some C or C++ code. So PHP can do anything that C or C++ can do.
  2. There are tens, if not hundreds of PHP extensions. Many of them are shipped built-in the main PHP distribution. If you need something that is not built-in PHP, you can always check the PECL PHP extension repository. This is the official repository for less popular PHP extensions written in C or C++ code.
  3. If there is no extension to do something that you need, maybe that is because you have an unusual need. Probably you are addressing a new problem or it is something that nobody else has a need for. So, you can always develop a new extension yourself. If you are not capable of developing C or C++ code, you can always hire another developer to do it for you. So you are never stuck without a solution.

PHP cannot do something that can be done in language X

  1. I doubt that there many relevant things that you cannot do in PHP that you can do in other languages. Maybe you can do things in other languages using different programming styles but that does not mean that you cannot develop the same features in PHP, given all the available PHP extensions.
  2. Still, if you find something that can only be done in some other language or you have to rely in existing components written in that language, you can always try to interface with code written in other languages using special PHP extensions available for that purpose.
  3. This is not a very well known fact, probably because it is not something that has great demand, but there PHP extensions that let you execute code in other languages from PHP scripts, like code in: Java, C# (.NET), Python, Perl, Lua, JavaScript using either V8 or SpiderMonkey engines.
  4. Oh, wait, there is no Ruby extension for PHP as you may have noticed. As I said, that is probably because nobody has such an odd need. Still, if you really have that need, maybe you can convert Ruby into Java using JRuby and then you can use the PHP Java extension to run the converted Ruby code. The same goes for other less popular languages.
  5. It is a long shot that may make you wonder if it really makes sense, but at least PHP does not leave you without a solution.

PHP is only for Web development

  1. PHP most common use is indeed for Web application development. Still you can run PHP outside a Web server using the PHP CLI (Command Line Interface) executable. It is a program that can be started from the command line shell for performing all sorts of operations, being Web site related or not.
  2. Even CPU intensive applications are developed in PHP and run outside a Web server using the PHP CLI program, like for instance sending newsletters to many subscribers. The PHPClasses itself sends millions of newsletter messages every month using the PHP CLI program.
  3. You can even create desktop applications to run on Windows, Linux, Mac or any other Unix flavor using the PHP-Gtk extension. You can also develop Windows specific applications or even Windows services using extensions like WinBinder and other PHP Windows specific extensions.
  4. It is not as if there is great demand for developing these kinds of operating system specific applications, but if you need them, you can develop them in PHP too if you want.

PHP is controlled by only one company (Zend)

  1. If you have already read about the PHP history you can realize that PHP was created by Rasmus Lerdorf in 1994. Over time Rasmus was joined by tens or even hundreds of other developers, including Andi Gutmans and Zeev Suraski, the founders of Zend, as well other core developers that work (or at least worked) for Zend.
  2. I think it is only natural that Zend developers will always try to influence PHP development, so it takes directions that are convenient to their business. After all they have invested their whole business in PHP. If PHP development takes a route that makes Zend irrelevant, it would probably kills their business.
  3. Still stating that PHP developement is controlled only by one company seems to be a great exaggeration. The fact is that PHP was developed and continues to be developed by many more people that are not associated with Zend.
  4. There are even core developers that work for Microsoft, like Pierre Alain Joye, a long time PHP core developer that was hired by Microsoft in the recent years to make PHP run well with Windows and other Microsoft products. There are also developers from Oracle, not just to take care of Oracle database extensions, but also MySQL, which is now also an Oracle product since they acquired Sun.
  5. The fact is that those PHP core developers that are connected to Zend, Microsoft, Oracle or other companies, are just a minority, despite their influence. The majority of core developers are not connected with any company.
  6. If you are concerned that those companies can be evil and try to influence the development of PHP to go in the wrong direction, rest assured that all the other core developers that are not tied to any company are also concerned and watching out for any wrong moves. If you tried to watch the discussions in the PHP internals list for a while you probably have already realized that.

PHP documentation is bad or insufficient

  1. If there is only one good thing about PHP, that is certainly the documentation. The documentation is well structured, clear and, given the necessary time to the documentation team, very complete. The user comments that appear in documentation pages only make it even more rich and complete.
  2. I have yet to see any other software project, Open Source or not, that has better documentation than PHP. The fact that it includes user comments qualify the PHP documentation project to be titled: Documentation 2.0 - used enhanced documentation.
  3. The PHP documentation team does an amazing job. Not only they take care of the main documentation in English, but they also have teams dedicated to translate it to tens of other idioms. That is certainly part of the reason why PHP is so popular everywhere in the world. Kudos to everybody that helps in the PHP documentation.
  4. Still the PHP documentation team cannot do miracles. The aspects that PHP covers are so extensive, that it is impossible to know every obscure detail that may make PHP work in a different way that it is expected in certain platforms. There can be bugs in PHP that may cause incorrect behavior, but that is not the fault of the PHP documentation team.
  5. That is why the user comments are so important. They provide additional information that lets other PHP users learn about unexpected behavior that the documentation team could not anticipate.
  6. Additionally, you may often find in documentation user comments sample code to perform certain tasks that PHP users wished that PHP functions could do. That makes PHP documentation with user comments even richer than it would be without the comments. Isn't that amazing?
  7. PHP projects are not reusable because they are not Object Oriented

    1. One of the reasons why PHP got so popular is the due to several killer applications that dominate the Web market. Several of them come to my mind like Wordpress, Drupal, Joomla, etc.. If you want to work as a PHP consultant, chances are that a good part of your clients will want you to integrate their sites with some of these applications.
    2. Nowadays Wordpress is certainly the most popular of the PHP killer applications. Recently, Matt Mullenweg, the creator of the Wordpress project, announced in his State of the Word 2011 speech that Wordpress is present in 14.7% of the top one million Web sites according to the W3 Techs Web survey. That is a lot!
    3. Matt also commented that many developers tweak their Wordpress installations with plug-ins to make it work as a CMS or eventually other types of applications.
    4. Still Wordpress code is mostly written in non-Object Oriented code. It ships with same base classes to implement some common functionality, but the core functionality is written in procedural code.
    5. This makes evident the fact that not being written with Object Oriented code is not necessary to make the project reusable, even for other purposes that are way beyond the original blogging platform purpose.
    6. But wait, do not get me wrong. This is the PHPClasses site. One of the rules that is mandatory to have PHP components accepted for publication in the PHPClasses site, is that the code that implements the described functionality must be written in the form of classes of Object Oriented code, thus the name of the site: PHP Classes.
    7. The reason for this requirement is that classes encapsulate functionality inside a container called a class. If component functions were global, there would be a greater chance of name clashing when combining multiple components of different sources.
    8. For instance, if two components had a function named "print" , how would the applications tell which of the components they want to call the function print? Classes make it easier to encapsulate functions with the same names within different scopes.
    9. But there is a workaround to avoid that problem without resorting to classes. You can just add a prefix to the functions of each component in order to avoid name clashing. For instance, the MySQL extension provides functions with the prefix mysql_.
    10. This is a very old solution used in the core PHP functions since ever, specially because until version 3 there was no Object Oriented support in PHP. But this practice was kept throughout the years even until today.
    11. Admittedly it is not an elegant solution. Component function name clashing (encapsulation) is just one of the benefits of Object Oriented Programming. But nobody can say that it does not work, or that it prevents PHP projects from being reusable.

    PHP is worse than Ruby On Rails, Python Django, X language Framework

    1. PHP comes with many extensions that provide many features but comparing a language with a full stack framework is like comparing pines with apples.
    2. It is fair to compare PHP with Java, C#, Ruby, Python, or "insert the language of the jour here". It is also fair to compare Ruby on Rails, Django, etc.. with a similar PHP framework. As a matter of fact there are so many similar frameworks in PHP, that we are not mentioning anyone in specific here to avoid being unfair to the fans of each of them.
    3. Personally we think that what the developers of any language need is not exactly to use a framework. What developers need is to embrace a methodology of development that makes them productive.
    4. Once you adopt a consist development methodology, everything becomes mechanic and you take less time to produce that same volume of work, as you just need to repeat the same development steps consistently.
    5. You do not really need to use a specific framework to adopt a consistent development method. For instance, we do not use any framework. we just follow the same development methodology that we have been evolving over the years, so nowadays we are quite productive using that methodology.
    6. We follow good development practices like separating concerns into different code components, but we do not need any MVC framework. Actually we feel that MVC is often an inadequate design pattern for use in the development of scalable Web applications.
    7. We usually separate application concerns in components that are can be distributed easily among different machines by the means of service layers. But that may be a subject for a different post.
    8. The main point here is that you do not need to use the framework X to be productive. What matters is that you follow a consistent development methodology that makes your work mechanic and fluent.
    9. The fact is that certain frameworks impose a certain development methodology. Those frameworks are being called "opinionated" because they reflect the opinion of their creators about how development should be.
    10. So, if you invested in studying a methodology that a certain framework imposes and that makes you productive when you implement your Web applications, fine, stick with it.
    11. But please do not come and tell that the framework X, of which you are a big fan, is the best solution, or worse like saying that PHP developers cannot be equally or more productive than you just because they do not use that other language framework that you love so much. That is just your opinion based just in your own experience. Do not underestimate other developers' experience.

    PHP is not good for high performance scalable Web sites or applications.

    1. The way I see it, performance and scalability are not a matter of language, but rather a matter of application architecture.
    2. When it comes to performance, as mentioned above, PHP is a compiled language, so its is speed is nowadays very good for most Web application purposes.
    3. Facebook is certainly the largest site that was developed in PHP. They do not seem to have scalability issues due to their adoption of PHP.
    4. It is true that they developed their own PHP to C++ compiler to make PHP applications run at top speed. It is also true that for CPU intensive applications, the gains of compiling PHP into a lower level language can be significant.
    5. However, the reality is that most Web applications are not CPU intensive. For instance, one of the activities that Web applications spend most time on is accessing databases.
    6. When your application executes a SQL query, most of the time is spent waiting for the database server to execute the query and return the results. Waiting for a query to execute on a database server in PHP or in a lower level language like C++ is going to take practically the same amount of time.
    7. So, now you may wonder, if PHP can be in practice equally as fast as C++ for database based Web applications, why did Facebook people went through such a great effort to develop a PHP to C++ compiler?
    8. The answer lies in the fact that they also changed the architecture. They do not just compile PHP into C++. They take the scripts of whole PHP applications, compile them into single ball of C++ code and generate a single executable that works as a multi-threaded Web server. Notice the emphasis on the multi-threaded word.
    9. Multi-threaded Web servers use a single process to handle many simultaneous HTTP requests. That saves them a lot of RAM because multiple threads share the same memory pool. This means that they end up needing less Web server machines to handle the same load. For a company like Facebook that has thousands of server machines, the gains are significant.
    10. For most other smaller Web sites, the gains are probably not significant enough to go through the effort of compiling PHP into C++.
    11. That does not mean you should not make an effort to educate yourself to learn and to adopt good architecture optimization techniques. Many of those techniques have been covered in this blog in the PHP performance articles category. Go and read them when you can.
    12. Still, most of those techniques are not language specific. You should adopt them regardless whether you develop your applications in PHP or other languages.

    PHP developers are cheaper because they are not qualified

    1. The economy works all in terms of offer and demand. If there is a product that is wanted by many customers and there is not enough quantity of the product on the market for sale, the prices tend to go up. On the other hand, if the product is abundant on the market and the customers are not buying it much, the prices tend to go down.
    2. The same goes for jobs. If there are more companies looking for qualified candidates than those that are available for hire, the offered salaries tend to go up. On the other hand, if there are more candidates than companies willing to hire them, the offered salaries may go down.
    3. The PHP market is huge because the Web is huge. There are many companies willing to hire qualified PHP Web developers. But for simpler jobs, they do not want to pay much because they can find plenty of candidates with sufficient qualifications. Those jobs can be as simple as installing and customizing existing PHP applications.
    4. But for companies that need to hire developers that not only know PHP, but also have other more sophisticated skills like application architecture planning and deployment, developing high scale Web sites, handle security matters properly, search engine optimization, etc., companies tend to pay much better salaries because developers with all those qualifications are scarce.
    5. So, it is not so much a matter a problem of the PHP developers. It is more a matter of the qualifications that are demanded by the types of PHP jobs that require PHP skills.
    Well, the list of misconceptions about PHP does not end here. We just listed a few that we found more important.
    A good professional should know more than just one language to eventually take more advantage of the opportunities that may show up in their careers. So our advice is that whether you love or hate PHP, do not just stick to PHP or that other language that you prefer instead of PHP.

No comments:

Post a Comment