
Learning PHP 7 High Performance
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
Key Features
Make the optimum use of PHP coding to improve your programming productivity
Leverage the potential of PHP for server-side programming, memory management, and object-oriented programming
Packed with real-life examples to help the readers implement concepts as they learn
Book DescriptionPHP is a great language for building web applications. It is essentially a server-side scripting language that is also used for general-purpose programming. PHP 7 is the latest version, providing major backward-compatibility breaks and focusing on high performance and speed. This fast-paced introduction to PHP 7 will improve your productivity and coding skills. The concepts covered will allow you, as a PHP programmer, to improve the performance standards of your applications. We will introduce you to the new features in PHP 7 and then will run through the concepts of object-oriented programming (OOP) in PHP 7. Next, we will shed some light on how to improve your PHP 7 applications' performance and database performance. Through this book, you will be able to improve the performance of your programs using the various benchmarking tools discussed. At the end, the book discusses some best practices in PHP programming to help you improve the quality of your code. What you will learn
? Setup high performance development and
production environment for PHP 7
? Discover new OOP features in PHP 7 to
achieve high performance
? Improve your PHP applications performance
? Attain improved database performance
? Benchmark PHP applications to optimize them
? Write quality code by learning to improve code
reusability, simplicity, and expressiveness
? Get rid of the bottlenecks in your PHP 7
applications by writing PHP code optimally
? Tackle issues related to web applications, such
as high user dependency and large datasets
Who this book is forThis book is for those who have basic experience in PHP programming. If you are developing performance-critical applications, then this book is for you.
All prices
More details
Other editions
Additional editions

Person
experience in PHP development. He received his degree in electrical engineering and
specialized in computer and communications from Pakistan. Altaf is an electrical
engineer on paper and a software engineer by heart.
He worked as a system programmer in his team, developing control software for
small test robots using assembly languages and C. After this, Altaf got interested
in web technologies and never looked back. He has worked with numerous PHP
frameworks, including Zend, Laravel, and Yii, and open source systems such
as Drupal, WordPress, PrestaShop, and Magento. Altaf designed and built two
proprietary CMS systems with full support for multiple languages and models,
permissions, and translations, as well as different kinds of multilingual content
management. Now, he works in the fashion industry as the head of IT at shy7lo.com,
where his role is to manage the development team on the premises and abroad, in
order to manage Magento and Laravel applications development and the deployment
life cycle. Besides web apps, Altaf has worked on iOS and Android applications,
including building APIs in Lumen. He is a big fan of service-oriented architecture
(SOA) and successfully uses it in different applications.
Altaf actively researches on website performance and has deployed the latest
technologies, such as PHP 7, NGINX, Redis, Varnish, and others, in production
environments for high-speed and scalable applications. He is a Debian lover and
uses it for all of his web application deployments.
When not working, Altaf writes articles for programmingtunes.com and
techyocean.com. He has reviewed several books for Packt Publishing, including
Learning Phalcon PHP, Mastering jQuery Mobile, and PrestaShop Module
Development. Raul Mesa has been writing software for the web since 2006. He started with Java, later moved on to PHP and obtained several Certifications such as Zend Engineer and Zend Framework. Having worked on several high traffic web projects he is nowadays very interested in the DevOps philosophy.
Raul works currently as a senior web developer on EuroMillions.com using DevOps and PHP technologies. He also leads various small to medium size projects.
His twitter: @rmrbest
I would like to thank my wife Noemi and my daughter Valeria for their support and their love and thank as well my father who bought my first computer back in 1992.
Content
Setting Up the Environment
New Features in PHP 7
Improving PHP 7 Application Performance
Improving Database Performance
Debugging and Profiling
Its time to Benchmark
Best Practices in PHP Programming
Appendix A: Tools to make the life easy
Appendix B: MVC and Frameworks
Chapter 2. New Features in PHP 7
PHP 7 has introduced new features that can help programmers write high-performing and effective code. Also, some old-fashioned features are completely removed, and PHP 7 will throw an error if used. Most of the fatal errors are now exceptions, so PHP won't show an ugly fatal error message any more; instead, it will go through an exception with the available details.
In this chapter, we will cover the following topics:
- Type hints
- Namespaces and group use declarations
- The anonymous classes
- Old-style constructor deprecation
- The Spaceship operator
- The null coalesce operator
- Uniform variable syntax
- Miscellaneous changes
OOP features
PHP 7 introduced a few new OOP features that will enable developers to write clean and effective code. In this section, we will discuss these features.
Type hints
Prior to PHP 7, there was no need to declare the data type of the arguments passed to a function or class method. Also, there was no need to mention the return data type. Any data type can be passed to and returned from a function or method. This is one of the huge problems in PHP, in which it is not always clear which data types should be passed or received from a function or method. To fix this problem, PHP 7 introduced type hints. As of now, two type hints are introduced: scalar and return type hints. These are discussed in the following sections.
Type hints is a feature in both OOP and procedural PHP because it can be used for both procedural functions and object methods.
Scalar type hints
PHP 7 made it possible to use scalar type hints for integers, floats, strings, and Booleans for both functions and methods. Let's have a look at the following example:
class Person { public function age(int $age) { return $age; } public function name(string $name) { return $name; } public function isAlive(bool $alive) { return $alive; } } $person = new Person(); echo $person->name('Altaf Hussain'); echo $person->age(30); echo $person->isAlive(TRUE);In the preceding code, we created a Person class. We have three methods, and each method receives different arguments whose data types are defined with them, as is highlighted in the preceding code. If you run the preceding code, it will work fine as we will pass the desired data types for each method.
Age can be a float, such as 30.5 years; so, if we pass a float number to the age method, it will still work, as follows:
Why is that? It is because, by default, scalar type hints are nonrestrictive. This means that we can pass float numbers to a method that expects an integer number.
To make it more restrictive, the following single-line code can be placed at the top of the file:
declare(strict_types = 1);Now, if we pass a float number to the age function, we will get an Uncaught Type Error, which is a fatal error that tells us that Person::age must be of the int type given the float. Similar errors will be generated if we pass a string to a method that is not of the string type. Consider the following example:
The preceding code will generate the fatal error as the string is passed to it.
Return type hints
Another important feature of PHP 7 is the ability to define the return data type for a function or method. It behaves the same way scalar type hints behave. Let's modify our Person class a little to understand return type hints, as follows:
The changes in the class are highlighted. The return type is defined using the: data-type syntax. It does not matter if the return type is the same as the scalar type. These can be different as long as they match their respective data types.
Now, let's try an example with the object return type. Consider the previous Person class and add a getAddress method to it. Also, we will add a new class, Address, to the same file, as shown in the following code:
The additional code added to the Person class and the new Address class is highlighted. Now, if we call the getAddress method of the Person class, it will work perfectly and won't throw an error. However, let's suppose that we change the return statement, as follows:
In this case, the preceding method will throw an uncaught exception similar to the following:
Fatal error: Uncaught TypeError: Return value of Person::getAddress() must be an instance of Address, array returnedThis is because we return an array instead of an Address object. Now, the question is: why use type hints? The big advantage of using type hints is that it will always avoid accidentally passing or returning wrong and unexpected data to methods or functions.
As can be seen in the preceding examples, this makes the code clear, and by looking at the declarations of the methods, one can exactly know which data types should be passed to each of the methods and what kind of data is returned by looking into the code of each method or comment, if any.
Namespaces and group use declaration
In a very large codebase, classes are divided into namespaces, which makes them easy to manage and work with. However, if there are too many classes in a namespace and we need to use 10 of them, then we have to type the complete use statement for all these classes.
Note
In PHP, it is not required to divide classes in subfolders according to their namespace, as is the case with other programming languages. Namespaces just provide a logical separation of classes. However, we are not limited to placing our classes in subfolders according to our namespaces.
For example, we have a Publishers/Packt namespace and the classes Book, Ebook, Video, and Presentation. Also, we have a functions.php file, which has our normal functions and is in the same Publishers/Packt namespace. Another file, constants.php, has the constant values required for the application and is in the same namespace. The code for each class and the functions.php and constants.php files is as follows:
Now, the code for the Ebook class is as follows:
The code for the Video class is as follows:
Similarly, the code for the presentation class is as follows:
All the four classes have the same methods, which return the classes' names using the PHP built-in get_class() function.
Now, add the following two functions to the functions.php file:
Now, let's add the following code to the constants.php file:
The code in both functions.php and constants.php is self-explanatory. Note that each file has a namespace Publishers/Packt line at the top, which makes these classes, functions, and...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: PDF
Copy-Protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (only limited: Kindle).
The file format PDF always displays a book page identically on any hardware. This makes PDF suitable for complex layouts such as those used in textbooks and reference books (images, tables, columns, footnotes). Unfortunately, on the small screens of e-readers or smartphones, PDFs are rather annoying, requiring too much scrolling.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our eBook Help page.