Skip to main content

Why and Where PHP Programmers Can Learn More about C and C++

In a previous post about  PHP and how much there is to learn about it beyond its typical uses. I talk about how to extend PHP with libraries written in C Language. As part of that talk the PHP-CPP framework is mentioned as a solution for simplifying development of PHP extensions in C++. But where is a PHP programmer to learn enough C++ to be dangerous?

Ten reasons for using PHP-CPP

There are many reasons for using PHP-CPP. Let's name a few.

 

1. It's fast

Code written in C++ is fast - much faster than code written in PHP. When you start using C++, you bring down your CPU and memory load, and your web pages instantly become more responsive. You can bring down the hardware costs, because you simply need less servers to handle the same number of page views.

 

2. It is shockingly simple

C and C++ have a reputation of being difficult and complex. But just take a look at the examples in our documentation and surprise yourself. The simple reality is that algorithms written in C++ look almost identical to algorithms written in PHP. If you know how to program in PHP, you can easily learn how to do the same in C++.

In fact, the syntax of PHP and most PHP function names are directly derived from the C and C++ syntax. Moving from PHP to C++ is like coming home. We have missed you, you have been away for too long.

 

3. No Zend engine knowledge required

Let's face the truth: the internals of the Zend engine are way too complex, and the code of the Zend engine is a mess, and mostly undocumented. You do not want to deal with that on a day to day basis. This has always been a good reason to stay far away from writing your own native extensions.

But the PHP-CPP library has encapsulated all these complicated structures in very easy to use C++ classes and objects. You can write amazingly fast algorithms using C++ without ever having to do a direct call to the Zend engine, and without even having to look at the Zend engine source. With PHP-CPP you can write native code, without having to deal with the internals of PHP.

 

4. Full documentation and annotated source

Many programmers find it a matter of honor to make code that can only be understood by themselves. We do not agree. The PHP-CPP library is fully documented (the documentation can be found on www.php-cpp.com/documentation), and the source code is full with comments and explanations.

If you ever need more information, or want to have a look at the source code, you can easily find your way (although the internal parts that deal with the Zend engine can sometimes be confusing, but that's because the Zend engine is so complicated).

 

5. Support for all the important PHP features

With PHP-CPP you can work with variables, arrays, functions, objects, classes, interfaces, exceptions and namespaces just as easily as you can with normal PHP scripts. Besides that, you can use all features of C++, including threads, lambdas and asynchronous programming.

 

6. You can't go faster than native

There are many different technologies under development that all try to speed up PHP. But nothing is ever going to beat native code. C/C++ is the fastest of all languages. Why would you choose for any of the other emerging technologies - if you can use the fastest of all languages too, which is is just as simple (if not simpler) as its alternatives?

 

7. Proven technology

C++ is a proven language with a more than 40 year long history. C++ has an official open standard and is controlled by a C++ standards committee with members that have proven track records. Compilers have been developed by companies like Microsoft, IBM, Intel, Apple and there are several open source compilers available (GNU, CLANG), so you can always switch to a faster or more stable alternative. The compiler vendors are constantly motivated to be better than their competitors and bring out new versions of their compilers all the time.

All this has lead to an extremely stable, powerful, clever and high quality programming language. The number of books and training courses about C++ is overwhelming, and this great C++ language can now also be used for building PHP extensions.

Alternative technologies to speed up PHP are not based on open standards, are controlled by single companies, do not have competing implementations, miss the long C++ heritage, and often only make your software more complex.

 

8. Access to an incredible amount of libraries

C/C++ is the most important language in the world, with the largest number of libraries. The moment you start writing code in C/C++ you immediately get access to this enormous amount of libraries. No other language can even come close to the number of libraries that are available in C/C++.

 

9. Working with C++ is fun

C++ is a great language that allows you to write brilliant object oriented code - but gives you at the same time the power to ruin everything. Working with C++ is like driving a very powerful sportscar: in the right hands it is the best car in the world, but also a dangerous weapon in the wrong hands. As a driver - you want such a car. As a programmer - you want C++.

 

10. IT IS FREE and Open Source!

PHP-CPP is an open source technology and free for you to use. You would be crazy not to try it. What are you waiting for? Download the library, install it on your system and get started with your first extension.

Sources for  books and learning c++ and C language.

Strict Beginners

  • Programming: Principles and Practice Using C++ (Bjarne Stroustrup) (updated for C++11/C++14) An introduction to programming using C++ by the creator of the language. A good read, that assumes no previous programming experience, but is not only for beginners.

Some Programming Experience

  • C++ Primer * (Stanley Lippman, Josée Lajoie, and Barbara E. Moo) (updated for C++11) Coming at 1k pages, this is a very thorough introduction into C++ that covers just about everything in the language in a very accessible format and in great detail. The fifth edition (released August 16, 2012) covers C++11. [Review]
  • A Tour of C++ (Bjarne Stroustrup) The "tour" is a quick (about 180 pages and 14 chapters) tutorial overview of all of standard C++ (language and standard library, and using C++11) at a moderately high level for people who already know C++ or at least are experienced programmers. This book is an extended version of the material that constitutes Chapters 2-5 of The C++ Programming Language, 4th edition.
  • Accelerated C++ (Andrew Koenig and Barbara Moo) This basically covers the same ground as the C++ Primer, but does so on a fourth of its space. This is largely because it does not attempt to be an introduction to programming, but an introduction to C++ for people who've previously programmed in some other language. It has a steeper learning curve, but, for those who can cope with this, it is a very compact introduction into the language. (Historically, it broke new ground by being the first beginner's book to use a modern approach at teaching the language.) [Review]
  • Thinking in C++ (Bruce Eckel) Two volumes; is a tutorial style free set of intro level books. Downloads: vol 1, vol 2. Unfortunately they’re marred by a number of trivial errors (e.g. maintaining that temporaries are automatically const), with no official errata list. A partial 3rd party errata list is available at (http://www.computersciencelab.com/Eckel.htm), but it’s apparently not maintained.
* Not to be confused with C++ Primer Plus (Stephen Prata), with a significantly less favorable review.

Best Practices

  • Effective C++ (Scott Meyers) This was written with the aim of being the best second book C++ programmers should read, and it succeeded. Earlier editions were aimed at programmers coming from C, the third edition changes this and targets programmers coming from languages like Java. It presents ~50 easy-to-remember rules of thumb along with their rationale in a very accessible (and enjoyable) style. For C++11 and C++14 the examples and a few issues are outdated and Effective Modern C++ should be prefered. [Review]
  • Effective Modern C++ (Scott Meyers) This is basically the new version of Effective C++, aimed at C++ programmers making the transition from C++03 to C++11 and C++14.
  • Effective STL (Scott Meyers) This aims to do the same to the part of the standard library coming from the STL what Effective C++ did to the language as a whole: It presents rules of thumb along with their rationale. [Review]

Intermediate

  • More Effective C++ (Scott Meyers) Even more rules of thumb than Effective C++. Not as important as the ones in the first book, but still good to know.
  • Exceptional C++ (Herb Sutter) Presented as a set of puzzles, this has one of the best and thorough discussions of the proper resource management and exception safety in C++ through Resource Acquisition is Initialization (RAII) in addition to in-depth coverage of a variety of other topics including the pimpl idiom, name lookup, good class design, and the C++ memory model. [Review]
  • More Exceptional C++ (Herb Sutter) Covers additional exception safety topics not covered in Exceptional C++, in addition to discussion of effective object oriented programming in C++ and correct use of the STL. [Review]
  • Exceptional C++ Style (Herb Sutter) Discusses generic programming, optimization, and resource management; this book also has an excellent exposition of how to write modular code in C++ by using nonmember functions and the single responsibility principle. [Review]
  • C++ Coding Standards (Herb Sutter and Andrei Alexandrescu) "Coding standards" here doesn't mean "how many spaces should I indent my code?" This book contains 101 best practices, idioms, and common pitfalls that can help you to write correct, understandable, and efficient C++ code. [Review]
  • C++ Templates: The Complete Guide (David Vandevoorde and Nicolai M. Josuttis) This is the book about templates as they existed before C++11. It covers everything from the very basics to some of the most advanced template metaprogramming and explains every detail of how templates work (both conceptually and at how they are implemented) and discusses many common pitfalls. Has excellent summaries of the One Definition Rule (ODR) and overload resolution in the appendices. A second edition is scheduled for 2016. [Review]

Advanced

  • Modern C++ Design (Andrei Alexandrescu) A groundbreaking book on advanced generic programming techniques. Introduces policy-based design, type lists, and fundamental generic programming idioms then explains how many useful design patterns (including small object allocators, functors, factories, visitors, and multimethods) can be implemented efficiently, modularly, and cleanly using generic programming. [Review]
  • C++ Template Metaprogramming (David Abrahams and Aleksey Gurtovoy)
  • C++ Concurrency In Action (Anthony Williams) A book covering C++11 concurrency support including the thread library, the atomics library, the C++ memory model, locks and mutexes, as well as issues of designing and debugging multithreaded applications.
  • Advanced C++ Metaprogramming (Davide Di Gennaro) A pre-C++11 manual of TMP techniques, focused more on practice than theory. There are a ton of snippets in this book, some of which are made obsolete by typetraits, but the techniques, are nonetheless useful to know. If you can put up with the quirky formatting/editing, it is easier to read than Alexandrescu, and arguably, more rewarding. For more experienced developers, there is a good chance that you may pick up something about a dark corner of C++ (a quirk) that usually only comes about through extensive experience.

Reference Books

  • The C++ Programming Language (Bjarne Stroustrup) (updated for C++11) The classic introduction to C++ by its creator. Written to parallel the classic K&R, this indeed reads very much alike it and covers just about everything from the core language to the standard library, to programming paradigms to the language's philosophy. (Thereby making the latest editions break the 1k page barrier.) [Review] The fourth edition (released on May 19, 2013) covers C++11.
  • C++ Standard Library Tutorial and Reference (Nicolai Josuttis) (updated for C++11) The introduction and reference for the C++ Standard Library. The second edition (released on April 9, 2012) covers C++11. [Review]
  • The C++ IO Streams and Locales (Angelika Langer and Klaus Kreft) There's very little to say about this book except that, if you want to know anything about streams and locales, then this is the one place to find definitive answers. [Review]
C++11 References:
  • The C++ Standard (INCITS/ISO/IEC 14882-2011) This, of course, is the final arbiter of all that is or isn't C++. Be aware, however, that it is intended purely as a reference for experienced users willing to devote considerable time and effort to its understanding. As usual, the first release was quite expensive ($300+ US), but it has now been released in electronic form for $60US
  • Overview of the New C++ (C++11/14) (PDF only) (Scott Meyers) (updated for C++1y/C++14) These are the presentation materials (slides and some lecture notes) of a three-day training course offered by Scott Meyers, who's a highly respected author on C++. Even though the list of items is short, the quality is high.

Background Classics

Note: Some information contained within these books may not be up-to-date or no longer considered best practice.
  • The Design and Evolution of C++ (Bjarne Stroustrup) If you want to know why the language is the way it is, this book is where you find answers. This covers everything before the standardization of C++.
  • Ruminations on C++ - (Andrew Koenig and Barbara Moo) [Review]
  • Advanced C++ Programming Styles and Idioms (James Coplien) A predecessor of the pattern movement, it describes many C++-specific "idioms". It's certainly a very good book and might still be worth a read if you can spare the time, but quite old and not up-to-date with current C++.
  • Large Scale C++ Software Design (John Lakos) Lakos explains techniques to manage very big C++ software projects. Certainly a good read, if it only was up to date. It was written long before C++98, and misses on many features (e.g. namespaces) important for large scale projects. If you need to work in a big C++ software project, you might want to read it, although you need to take more than a grain of salt with it. The first volume of a new edition is expected in 2015.
  • Inside the C++ Object Model (Stanley Lippman) If you want to know how virtual member functions are commonly implemented and how base objects are commonly laid out in memory in a multi-inheritance scenario, and how all this affects performance, this is where you will find thorough discussions of such topics.


The above list is purposely C++ flavored. If you really want to learn from the root language of C then here's a list of beginners books.

C Language Beginners

  • Programming in C (3rd Edition) - Stephen Kochan
  • C Primer Plus - Stephen Prata
  • C Programming: A Modern Approach - K. N. King
  • A Book on C - Al Kelley/Ira Pohl
  • The C book - Mike Banahan, Declan Brady and Mark Doran
  • Practical C Programming, 3rd Edition - Steve Oualline
  • C: How to Program (6th Edition) - Paul Deitel & Harvey M. Deitel
  • Head First C - David & Dawn Griffiths
I am not a big fan of the "learn a new programming language every year" doctrine. I think it should be "learn something new to you every year". Adding C++ a to both your PHP tool box and learning about the language itself will fill your time with many years of new things.

Comments