Exciting Features to Look Forward in PHP 7.4

Have you ever wonder? what’s new in PHP. After PHP 7, PHP 7.4 is about to release on November 28th, 2019. No alpha or beta version, PHP 7.4 will be getting released as General Availability in the market.

The most exciting part will be to see the new features in this version that will make PHP website development much reliable and faster. 

Though it has already been approved that PHP 8 is the real milestone for PHP performance, still it’s quite far from being released. 

php 7.4

Here we will be focusing on some of the new features that you will be getting with PHP 7.4.

Constructors & Defaulters

Let’s take a keen look at how typed values are initialised. If the value is scalar types, it is possible to provide the default value: 

class Foo{public int $bar = 4;
public ?string $baz = null;
public array $list = [1, 2, 3];}

If the type is nullable then you can only use “null” as a default. Well, this is quite obvious to understand but there is some uneven behaviour with the parameter defaults where the following is included:

function passNull(int $i = null)
/* … */ } passNull(null);

Fortunately, this uneven behaviour with typed properties is not allowed. Also with “object” or class types, it is quite impossible to get default values. To set their defaults, you will have to use constructors. To initialise the typed values, usage of constructors is obvious:

class Foo
{
private int $a;public function __construct(int $a)
{
$this->a = $a;
}
}

However, you will have to make sure that writing uninitialised property outside the constructor is valid. The uninitialised check will not perform until there is something to read within the property.

Uninitialised

Is the following code valid or not?

class Foo
{
public int $bar;
}
$foo = new Foo;

PHP will display an error message when “$bar” is accessed even if its value is not an integer after making an object of “Foo”:

var_dump($foo->bar);
Fatal error: Uncaught Error: Typed property Foo::$barmust not be accessed before initialization

As you can see there is a new type of “State Variable” called uninitialised.

The value will be “null” if the “$bar” does not have any value. It is impossible to acknowledge whether the type nullable property was set or is forgotten if the “types” are nullable. This is because the new variable “uninitialised” will be introduced in PHP 7.4.

Let’s take a look at some of the major points to remember about uninitialised:
  • Reading from uninitialised properties will result in displaying a fatal error message.
  • If the type is non-nullable, you will be able to create an object within the uninitialised property as this state is verified while accessing the property.
  • Before reading, you can create an uninitialised property.
  • Unsetting an untyped property will make the value “null” but if using “unset” on a typed property then the value will be uninitialized.

You can keep a note on the below-mentioned code where the non-nullable and uninitialised property is set just after creating the object is valid:

class Foo
{
public int $a;
}
$foo = new Foo;
$foo->a = 1;

Type validation is done at the time of writing and uninitialised state is only analysed at the time of reading the value of the property. This makes it sure that no invalid type will end up as a property’s value.

Inheritance & Type Variance

It does not matter if PHP 7.4 has improved type variance, the typed properties are still the same i.e., invariant. For a better explanation, take a look at the following code:

class A {}
class B extends A {}
class Foo
{
public A $prop;
}
class Bar extends Foo
{
public B $prop;
}
Fatal error: Type of Bar::$prop must be A (as in class Foo)

If the above-mentioned example does not make any difference then consider taking a look at the following:

class Foo
{
public self $prop;
}
class Bar extends Foo
{
public self $prop;
}

This new version of PHP will be replacing “self” in the backend with the child class a while before executing the code. The only solution to handle it is mentioned below:

class Foo
{
public Foo $prop;
}
class Bar extends Foo
{
public Foo $prop;
}

If we talk about inheritance, it will be difficult for you to come up with any better use of cases to replace the types of inherited properties.

It is quite strange to see that it is possible to change the type of every individual inherited property. However, it is only possible if the access modifier is changed from “private” to “protected” or “public”.

Take a look at the following valid code:
class Foo
{
private int $prop;
}
class Bar extends Foo
{
public string $prop;
}

But, changing the type from nullable to non-nullable or vice-versa is not allowed in this version of PHP i.e., 7.4.

class Foo
{
public int $a;
public ?int $b;
}
class Bar extends Foo
{
public ?int $a;
public int $b;
}

Fatal error: Type of Bar::$a must be int (as in class Foo)

Types

Let’s see what can be typed and in which form. However, make sure that typed properties work underclasses. For making them work, access modifier or keyword “var” is required in front of them.

In PHP 7.4, almost every type can be used excluding “void” & “callable”. “void” refers to the absence of a value which is quite clear that it cannot be used to type any value. Similarly with “callable”, it seems nuanced.

The following code shows how a “callable” looks like in PHP:

class Foo
{
public callable $callable;public function __construct(callable $callable)
{ /* … */ }
}
class Bar
{
public Foo $foo;
public function __construct()
{
$this->foo = new Foo([$this, ‘method’])
}
private function method()
{ /* … */ }
}
$bar = new Bar; ($bar->foo->callable)();

Here, “callable” is a private “Bar: :method”, though it is contexting the “Foo”. Due to this problem, PHP 7.4 do not include “callable” support.

However, it’s not a big deal as “Closure” is included as a valid type in PHP 7.4 that will call “$this” context when required.

Here’s a list of types that will be available in PHP 7.4:

● int
● float
● bool
● aray
● string
● iterable
● object
● self & parent
● ? (nullable)
● Classes & interfaces

Strict Types & Coercion

PHP is known to be a dynamic programming language to which many developers loves and even hate. If you are passing a string where you expect it an integer, current PHP will approach and will convert it into a string automatically:

function coerce(int $i)
{ /* … */ }coerce(‘1’); // 1

The same applies on the typed properties. Take a look at the following valid code which is converting “1” to 1.

class Bar
{
public int $i;
}
$bar = new Bar;
$bar->i = ‘1’; // 1

The best thing is if you do not want this behaviour, you can simply disable it by applying strict types:

declare(strict_types=1);
$bar = new Bar;
$bar->i = ‘1’; // 1

Fatal error: Uncaught TypeError:
Typed property Bar::$i must be int, string used

Well, these are some of the important types of properties that you will see in the new update of PHP i.e., PHP 7.4 GA release. Feel free to contact us to know more about PHP web development service.

Benefits of custom PHP web development

Let’s just say that PHP is the WhatsApp of scripting languages. It is widely used, open-source, easy, free, available on all platforms and to exaggerate a bit, even a 5-year-old can be taught how to use it.

PHP is used by many major web companies like Facebook, Twitter, LinkedIn, WordPress, etc. If you want to make your own website or start a new venture, PHP is the way to go. And today, when a technological revolution is taking place in the world, e-commerce and online marketing companies are being set up at a very high rate. A well-functioning website is an elementary thing to do; as necessary for a company as a healthy diet is necessary for a person to stay fit.

PHP-WEBSITE-DEVELOPMENT-A

Now, the real question is what are the benefits of PHP web developing? Why do you hear so much about PHP and not about any other scripting language?

For a moment, let’s just forget about PHP or web developing and go back to the ’70s, a time when technology was just climbing atop the hill of progress. During these times, communication methods were more inclined towards personality rather than the actual exchange of information. Let’s say there’s a person, an outsider, who does not know how to speak or communicate with people. He is a tribal who does not know the common tongue.

But that guy has an idea that would make him a millionaire. Now, In order to implement his ideas he has to talk to several people in different countries who speak different languages. In order to communicate with them, what should he do? How should he develop himself so that he can grab the best out of the situation? Obviously, first of all, he should write down his idea, put down a presentation, a nice one which everybody would understand and appreciate.

Now, he does not know any language so what languages should he learn? It’s obvious that everyone would tell him to learn English as it’s a language used globally to interact with people who speak a foreign language.

 If he would present himself in English, all the big powers, the Chinese, French, German, Indian, Russian and of course American, everyone would understand him and might dig deep into their pockets for the tribal’s idea. Another good thing about English is that it is not as diversified as Hindi or Chinese, not as hard to pronounce as French or German, so the person himself would be happy to learn it. So once that person learns English, he can have no regrets and can do business with utmost ease & support.

Language is the road map of a culture. It tells you where its people come from and where they are going. ‒Rita Mae Brown

The same is with the PHP language; it comes with all the good things you need in a scripting language. It is easy as it is based on C/C++, a large community of developers is present to update and document the language, it is secure, supports almost all platforms & servers, is used by a lot of big companies around the world and also supports RAD (Rapid Application Development).

php-development

Custom web development is even more beneficial as it makes you choose on your own. When you visit a website, see things that annoy you and you don’t want them on your website, you can go with custom web development. Instead of having a standard website, you can customize it according to your needs. And the easiest way to do this would be by using PHP.

Customized PHP web development is very effective and yields good results for business enterprises, regional companies and small scale companies too. These websites are of high quality, unique and perform well as they can handle a lot of traffic. Hiring brilliant and intelligent developers is also not a big deal today. You don’t have to go running around looking for them as they are right here on the internet, just a click away. It is very convenient to hire them too as they can be hired on an hourly basis. Ultimately, you’ll have lucrative results at cheap investments.

In conclusion, using PHP for web developing is very beneficial. Custom web development plays a crucial role when you want a perfect website in accordance with your needs. And in today’s world when technology’s drums are thumping their beats on every corner of the Earth, you should have the right musical notes or you’ll get out of tune and the ones running the tunes of custom PHP development will win. Custom PHP Web development is your Whiplash!