In the last chapter, we discussed the session options feature along with their examples. In this chapter, we are going to discuss the various PHP features that are Deprecated in PHP 7 release
Deprecated means ‘to express disapproval of’. Therefore, these features were approved to be in use for implementation in the past, but are now deprecated in PHP 7 programming. If we still use these features in PHP 7 programming, then we will find them to be operational but with the deprecated warning at the time of program execution. Following are the deprecated features in PHP 7.
1. PHP 4 style constructors
PHP 4 style constructors are methods which have the same name as that of class, where they are defined. These methods are deprecated now and will eventually be removed permanently in the future releases of PHP 7.x. If we use PHP 7 constructor as the only constructor defined in a class, then PHP 7 will emit E_DEPRECATED. The classes which are implementing __construct () method will remain unaffected.
In the following example, we have defined a class with the name as ‘AClass’ that has a method constructor with the same name as that of class. Such method is also known as PHP 4 style constructor.
When we run the above PHP 7 program, then the following output will be observed. Here, we can see the deprecated warning along with the PHP 4 style constructor print message. Later in this PHP 7 program, we are instantiating an object of AClass’ class.
2. Static call to a non-static method
In the earlier versions of PHP, we could make a static call to a method that is non-static. However, onward from PHP 7, this feature is deprecated and will eventually be removed permanently in future releases of PHP 7.x.
In the following example, we have defined a class with the name as ‘AClass’, that has a non-static method ‘bFunction ()’ that is enclosing a print statement. Later in this PHP 7 program, we will make a static call to this non-static method.
When we run the above PHP 7 program, then the following output will be observed. Here, we can observe the deprecated warning along with the print message made by non-static call.
3. password_hash () salt option
In the earlier versions of PHP, we had ‘password_hash ()’ function that had a salt option. This salt option is now deprecated because the developers’ own salt options were insecure. This function ‘password_hash ()’ generates a cryptographically secure salt by itself in the case when no salt is given by the PHP developer. Therefore, there is no need of custom salt generation anymore for the program to become more secure.
In the following example, we are using the ‘password_hash ()’ function that creates a password hash. Following is the syntax for this function.
password_hash (string $password, integer $algo, array $options)
It accepts following three parameters.
- String $password: This is the first parameter that accepts the actual password string to be hashed.
- Integer $algo: It accepts the integer value for the type of hashing algorithm. Examples are PASSWORD_DEFAULT, PASSWORD_BCRYPT, etc.
- Array $options: This parameter is the optional parameter that accepts an array of custom salt function from developer, cost, etc. This is the actual feature that is deprecated for ‘password_hash ()’ function in PHP 7, as it may result in generating insecure salts based on the developer’s code. Later versions of PHP 7 are expected to remove this parameter permanently. Thus, only above two parameters will exist in future release of PHP 7.x.
We are using the PASSWORD_BCRYPT for the algo parameter. It will result in the password parameter that is being truncated to a maximum length of 72 characters as shown in the output.
When we run the above PHP 7 program, then the following output is observed. Here, we can observe the salt is being truncated to a maximum length of 72 characters.
4. capture_session_meta SSL context option
In the earlier versions of PHP 7, capture_session_meta context option was used has now been deprecated in PHP 7. SSL metadata is now assessed through the ‘stream_get_meta_data ()’ function. It has the following syntax.
stream_get_meta_data (resource $stream)
Description: This function is used to retrieve the header or metadata from streams or file pointers e.g. SSL metadata, etc.
Parameters: It accepts only one parameter of type resource $stream. The stream could be any stream created by fopen, fsockopen and pfsockopen.
Returns: This function returns an array. The result array contains the following items:
- timed_out (bool): It will be returned if the stream gets timed out while waiting for data on the last call to fread or fgets.
- blocked (bool) – It will be returned if the stream is in blocking IO mode.
- eof (bool) – It will be returned if the stream has reached end-of-file.
- unread_bytes (int) – This is the integer value for the number of bytes currently contained in the PHP’s own internal buffer.
This returned value shouldn’t be used in the following script.
- stream_type (string) – A label that describes the underlying implementation of the stream.
- wrapper_type (string) – A label that describes the protocol wrapper implementation layered over the stream.
- wrapper_data (mixed) – Wrapper specific data attached to this stream.
- mode (string) – the type of access required for this stream.
- seekable (bool) – Boolean value whether the current stream can be made seekable.
- uri (string) – The URI or filename that is associated with this stream.
Source Code for the deprecated feature in PHP 7
In this chapter, we have discussed the various PHP features that are deprecated in PHP 7 release as oppose to PHP 4 and 5.x. In the coming chapter of this tutorial, we are going to discuss the various old extensions and SAPIs which are removed in PHP 7 and the new classes and interfaces that are added to PHP 7.
The post Learn How to Use various PHP features that are deprecated in PHP 7 appeared first on Eduonix.com | Blog.