PHP 8 Ready Or Not

Added 6 months ago By Stephen Ilori

Tags

My name is Ilori Stephen Adejuwon and I am a fullstack software developer in West Arica, Lagos Nigeria. In this article I will be talking about what to expect in PHP 8 which is scheduled to be released later this year.

Before we start talking about the features we are most likely to see in PHP 8, I would like us to do a little time travel and talk about the history of PHP.

History Of PHP

It was created in the year 1994 by Rasmus Lerdof, the very first incarnation of PHP was a simple set of "Common Gateway Interface Binaries" which was written in the 'C' programming Language. During that time, the sole purpose of why Rasmus created PHP was to track visits to his own online resume which birth the name Personal Home Page Tools, but we feel a lot comfortable calling it Personal Hypertext Processor.

Since it's inception, the language has grown to become something very popular and has proven useful to major companies like Facebook, wikipedia, Yahoo, Mailchimp, Tumblr, Slack and Dailymotion.

Even with this, there is still a lot of controversy over the language concerning areas such as performance, speed and security. But with PHP 8 just around the corner, all these are about to change, Although in PHP 7 we have seen some great improvements in performance and speed as a PHP 7 script is 3 times faster than a python script.

Let's get to what we are here for. The features we are likely to see in PHP 8. PHP 8 is still in active development also more proposals would be made over time but I would always make out time to update this article. For now, I have added 5 of the features we should expect from the coming version.

Features To Expect In PHP 8

  1. UNION TYPES:

    These are collections of two or more types indicating that either one of them can be used. Union types are specified by using the pipe operator "|" and they can be used in any position where types are currently accepted. Types can be associated with functions or return values. I.e

        function helloWorld(array $someArray) {  }
    
        function anotherFunction() : array { }
    
        class Codelighters {
            private float $total;
        }
    

    These declarations perform two useful operation.

      • They allow the PHP engine to enforce the correct type of variable passed to or returned from a function.
      • They make it easier to reason about what type needs to be passed to or returned from a function. Both human or static code analysis can use this information to help determine the correctness of a code.

    Union types are specified using the syntax T1|T2|… and can be used in all positions where types are currently accepted. I.e

        class Number {
            private int|float $number;
    
            public function setNumber(int|float $number): void {
                $this->number = $number;
            }
    
            public function getNumber(): int|float {
                return $this->number;
            }
        }
    

    Union types support all types currently supported by PHP, but there are some exceptions such as the following;

    1. The Void Type:

      The void type can never be part of a union. As such, types like T|void are illegal in all positions, including return types. What is likely intended instead is ?T, which allows returning either T or null.

    2. Nullable Union Types:

      Union types and the ?T nullable type notation cannot be mixed. Writing ?T1|T2, T1|?T2 or ?(T1|T2) is not supported and T1|T2|null needs to be used instead.

    3. The false pseudo-type:

      The false pseudo-type cannot be used as a standalone type (including nullable standalone type). As such, all of false, false|null and ?false are not permitted.

    4. Duplicate and redundant types:

      To catch some simple bugs in union type declarations, redundant types that can be detected without performing class loading will result in a compile-time error. This includes:

      • Each name-resolved type may only occur once. Types like int|string|INT result in an error.
      • If bool is used, false cannot be used additionally.
      • If object is used, class types cannot be used additionally.
      • If iterable is used, array and Traversable cannot be used additionally.
          function foo(): int|INT {} // Disallowed
          function foo(): bool|false {} // Disallowed
      
          use A as B;
          function foo(): A|B {} // Disallowed ("use" is part of name resolution)
      
          class_alias('X', 'Y');
          function foo(): X|Y {} // Allowed (redundancy is only known at runtime)
      
  2. JIT (Just In Time)

    PHP is an interpreted language so it parses code and compiles them on the fly which makes it significantly slow as compared to compiled languages.

    JIT is a way of executing computer code that involves compilation during execution of a program at runtime rather than before execution into a form that's usually faster typically the host cpu's instruction set.

    This is still a proposal for PHP 8. However, using JIT may open the door for PHP being used more frequently in other non-web, CPU-intensive scenarios where the performance benefits would be actually substantial and would also definitely change the way people see the language.

  3. The str_contains function

    With str_contains, it means we don't need to devise other methods or use the strpos in checking whether a string exists. We just do this instead.

        if (str_contains('php 8 is awesome', 'awesome')) { /* … */ }
    

    And if you ask me, this looks like LARAVEL \Str::contains() method.

  4. The get_debug_type function

    The get_debug_type() returns the type of a variable. We could have used the gettype() function instead right? Well, the get_debug_type() returns more useful output for arrays, strings, anonymous classes and objects.

    For example, calling gettype() on a class would return an object. Using get_debug_type() will return the class name.

  5. The fdiv function

    The new fdiv() function does something similar as the fmod() and intdiv() functions, which allows for division by 0. Instead of errors you'll get INF, -INF or NAN, depending on the case.

So for now, here are the 5 coming features to expect in PHP 8. I will always come back to update this article with the rest of the features promised by PHP 8. You can visit their official website php.net to get a full list of the features being proposed for the language. You can follow me on these websites with love;

Once again, my name is Ilori Stephen. Don't forget to upvote and share this article.

PHP 8 Ready Or Not was Authored by Stephen Ilori
Get to know more about Stephen Ilori
Author's Avatar

Stephen Ilori

I am a fullstack web developer in Lagos igeria with 1 + year working experience. I enjoy building software applications for both myself and my firm.

See All Stephen Ilori Articles

All Tags

If you are looking for more, browse through our available tags.

Frontend Backend Fullstack Devops Mobile Career
0
331
0

Subscribe To Our Newsletter

Subscribe to our newsletter... Get weekly helpful tips on programming and video updates... We promise never to bug you. Just about two to three messages per week.