0 0
Read Time:6 Minute, 8 Second

Why PHP in the first place?

  • it’s a widely-used, open-source scripting language
  • it’s executed on the server
  • it’s free to download and use
  • It’s used to build/manage dynamic content, databases, session tracking, e-commerce sites
  • Approx 78% of all websites run completely/partially with PHP as a server-side language

But one thing that stands apart from other server side programming languages is that PHP is one of the programming languages that is the easiest to learn and adapt.

The current version of PHP i.e. 7 feels familiar to its predecessors PHP 5.4 but has been optimized and tuned for performance. Version 8 on the other hand further improves on this and introduces other new features.

PHP 8 is doing its alpha testing circles around the web for quite some time now and is expected to be released in December 2020, and it’s bringing a bunch of new features and language improvements.

What you should expect from this release?

As PHP 8 is a major release, one should expect your old code would no longer be compatible. But most of these complications were already addressed in versions 7.x releases. So if your current code is up-to-date then you won’t have a problem in migration.

Let’s see some of the upgrades:  

  1. Backward Incompatible Changes
  2. Changes in SAPI modules
  3. Deprecated Functionality
  4. New Functions
  5. New Classes and Interfaces
  6. Removed Extensions and SAPIs
  7. New Global Constants
  8. Changes to INI File Handling
  9. Windows Support
  10. Performance Improvements

complete list of changes can be found in the upgrade notes on GitHub

Top 8 Features in the New Version of PHP 8:

  1. Preloading  
    PHP can be configured to preload scripts into the opcache when the engine starts. Any symbols (functions, classes, etc.) in those files will then become globally available for all requests without needing to be explicitly included. Note that the optimal tradeoff between performance and memory may vary with the application. preload.php is an arbitrary file that will run once at server startup (PHP-FPM, mod_php, etc.) and load code into persistent memory i.e. any file referenced by includeinclude_oncerequirerequire_once, or opcache_compile_file()
    opcache.preload=preload.php
  2. OPcache Extension
    OPcache has been recently improved with the implementation of preloading. Being an interpreted language, PHP needs an interpreter which leads to increased use of computer resources but also takes a lot of time. CPU resources are often the most strained. Preloading provides a way to store a specified set of scripts into OPcache memory “before any application code is run”

  3. JSON extension activated 
    The PHP native JSON extension has been bundled and compiled with PHP by default since 5.2 but it needs to be manually activated from the INI. However, from PHP 8 this extension will be activated at all times. and is assumed that this simplifies working with PHP developers.
    You would also notice some noticeable changes related to more strict compliance with the JSON RFC.

  4. Constructor property promotion

    Constructor Property Promotion is a shorthand syntax to declare and assign class properties from the constructor.  It allows you to combine class fields, constructor definition and variable assignments all into one syntax.
    P
    romoted parameters may occur inside trait constructors. This avoids having to type the class property name from 4 times to just once, and property type from twice to just once
    Promoted properties have to be prefixed by one of the visibility keywords i.e. public, private, protected

    class Animal{
        public function __construct(private string $type) {
            echo $this->type;
        }
    }
    
    new User('Kittie');
    # echos "Kittie"
  5. Match expression  
    A powerful feature that will often be the better choice to using switch. Although the name suggests a certain link with regular expressions, it is rather close to the switch command.
    Thanks to this mechanism, developers will be able to return the value based on the input parameter, without using additional keywords, i.e. break, return.
    What’s more, Match uses strong typing to compare values (similar to using the === comparison expression), and if none of the cases are met, the UnhandeledMatchError exception is thrown by default.
    switch ($statusCode) { 
    	case 200: 
    		$message = "DynamicContent"; 
    	break; 
    	case 301:
    	case 302:
    	case 303: 
    		$message = "RedirectPage"; 
    	break;  
    	case 404: 
    		$message = "Not found"; 
    	break; 
    	default: 
    		$message = "Unknown status code"; 
    	break; 
    }
    Here’s its match equivalent:
    match ($status_code) { 
    	200 => "DynamicContent", 
    	301, 302, 303 => "RedirectPage", 
    	404 => "PageNotFound", 
    	default => "unknown status code"  
    };
  6. Weak Maps
    Weak maps allow creating a map from objects to arbitrary values without preventing the objects that are used as keys from being garbage collected.
    WeakRefs were already added in PHP 7.4. With PHP 8, however, WeakMaps have been included which are the extension of this function. This will allow a better way of managing and processing its objects.
    All this will increase productivity by reducing the waiting time for a request for example. WeakMaps and WeakRefs can be used to delete objects when only the cache references the entity classes of the objects. 
    $map = new WeakMap;
    $obj = new stdClass;
    $map[$obj] = 42;
    var_dump($map);
    
    /*
    Output:
    object(WeakMap)#1 (1) {
    	[0]=>
    		array(2) {
    			["key"]=>
    				object(stdClass)#2 (0) {
    				}
    			["value"]=>
    				int(42)
    		}
    }
    */
    
    // The object is destroyed here, and the key is automatically removed from the weak map.
    unset($obj);
    
    var_dump($map);
    /*
    Output:
    object(WeakMap)#1 (0) {
    }
    */
  7. JIT (Just In-Time Compiler)
    Among the major new features of PHP 8, is the JIT compiler, which promises to improve performance significantly. As we know PHP is not compiled, but interpreted line by line, the JIT translates the extreme parts of the intermediate code into machine code.
    However, these improvements would not be noticeable in the context of web requests, but rather in heavy data-driven usage.
  8. Union Types
    Union Types are well known concept in programming languages like C/C++, TypeScript etc. Union types can be useful in a variety of cases and are a collection of two or more types which indicate both for input and output data. This will also allow several types to be defined for the arguments received by a function, as well as for the value it returns.
    Until now, the syntax of the language did not allow for the use of the Union but this has been solved by defining types in annotations.
    class Animal
    { 
    	private int|string $type; 
    	
    	public function setType(int|string $type): void { 
    		$this->type = $type; 
    	} 
    	public function getType(): int|string { 
    		return $this->type; 
    	} 
    }
    

Conclusion

PHP 8 may not be the monster of an update like PHP 7.0-7.4 was, but PHP 8 is a big step towards tidying up the language and a release that brings many long-awaited features. 

Though in this article I have tried to explain some of the important features and upgrades that I feel is important to understand, there are many more that might feel right and that fits your context of implementation. (Read more on https://php.watch/versions/8.0)

However, JIT compiler opens up new horizons for the language, I have kept it brief and I would love to talk about JIT compiler in a separate article.

As a developer, it’s a challenge to use all the best functions and optimization techniques in our development cycle, but I would love to hear about What part of the PHP 8 update will you be using the most? 

About Post Author

Austin Noronha

Howdy! This is Austin Noronha I am fullstack developer having 10x experience in different technologies and frameworks. I love exploring UI/UX & design patterns. Also learning SEO, SEM & Digital Marketing. My passion Foodie + Homecook #talesofacoder #whatsbuzzing
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleppy
Sleppy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Menu
Social profiles
Close