Hussain | Learning PHP 7 High Performance | E-Book | www.sack.de
E-Book

E-Book, Englisch, 202 Seiten

Hussain Learning PHP 7 High Performance

Improve the performance of your PHP application to ensure the application users aren't left waiting
1. Auflage 2025
ISBN: 978-1-78588-163-3
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)

Improve the performance of your PHP application to ensure the application users aren't left waiting

E-Book, Englisch, 202 Seiten

ISBN: 978-1-78588-163-3
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)



PHP 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.

Hussain Learning PHP 7 High Performance jetzt bestellen!

Autoren/Hrsg.


Weitere Infos & Material


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 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 years; so, if we pass a float number to the method, it will still work, as follows:

echo $person->age(30.5);

Why is that? It is because, by default, . 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 function, we will get an Uncaught Type Error, which is a fatal error that tells us that 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:

echo $person->isAlive('true');

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 class a little to understand return type hints, as follows:

class Person { public function age(float $age) : string { return 'Age is '.$age; } public function name(string $name) : string { return $name; } public function isAlive(bool $alive) : string { return ($alive) ? 'Yes' : 'No'; } }

The changes in the class are highlighted. The return type is defined using the 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 class and add a method to it. Also, we will add a new class, , to the same file, as shown in the following code:

class Address { public function getAddress() { return ['street' => 'Street 1', 'country' => 'Pak']; } } class Person { public function age(float $age) : string { return 'Age is '.$age; } public function name(string $name) : string { return $name; } public function isAlive(bool $alive) : string { return ($alive) ? 'Yes' : 'No'; } public function getAddress() : Address { return new Address(); } }

The additional code added to the class and the new class is highlighted. Now, if we call the method of the class, it will work perfectly and won't throw an error. However, let's suppose that we change the return statement, as follows:

public function getAddress() : Address { return ['street' => 'Street 1', 'country' => 'Pak']; }

In this case, the preceding method will throw an exception similar to the following:

Fatal error: Uncaught TypeError: Return value of Person::getAddress() must be an instance of Address, array returned

This is because we return an array instead of an 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 namespace and the classes , , , and . Also, we have a file, which has our normal functions and is in the same namespace. Another file, , has the constant values required for the application and is in the same namespace. The code for each class and the and files is as follows:

//book.php namespace Publishers\Packt; class Book { public function get() : string { return get_class(); } }

Now, the code for the class is as follows:

//ebook.php namespace Publishers\Packt; class Ebook { public function get() : string { return get_class(); } }

The code for the class is as follows:

//presentation.php namespace Publishers\Packt; class Video { public function get() : string { return get_class(); } }

Similarly, the code for the class is as follows:

//presentation.php namespace Publishers\Packt; class Presentation { public function get() : string { return get_class(); } }

All the four classes have the same methods, which return the classes' names using the PHP built-in function.

Now, add the following two functions to the file:

//functions.php namespace Publishers\Packt; function getBook() : string { return 'PHP 7'; } function saveBook(string $book) : string { return $book.' is saved'; }

Now, let's add the following code to the file:

//constants.php namespace Publishers/Packt; const COUNT = 10; const KEY = '123DGHtiop09847'; const URL = 'https://www.Packtpub.com/';

The code in both and is self-explanatory. Note that each file has a line at the top, which makes these classes, functions, and...



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.