Return Type Declarations – new feature PHP7

This is another example to all those developers. who doesn’t want to update their self with the latest technology?

I have just come across a new functionality that implemented in PHP7.
Although I am PHP 7.1 User but never put effort to check the new functionality of PHP7.
But I think somewhere it’s fine to check and explore things when they come in your use.
But better Somewhere it’s much fine to check new technology updates in your free time ūüėÄ

What is return type declaration?

This Functionality adds an optional return type declaration to function declarations.

function foo(): array {
    return [];

You can explore more Examples here.

How I come across?

I was exploring PHP Design patterns.
I checked code here

namespace DesignPatterns\Creational\AbstractFactory;

 * In this case, the abstract factory is a contract for creating some components
 * for the web. There are two ways of rendering text: HTML and JSON
abstract class AbstractFactory
 abstract public function createText(string $content): Text;

Everything was fine for me but abstract function declaration seems quite different.
Just for now the : Text was new. I checked several codes but didn’t find any specific solution. I checked the repo code itself but got Text is another abstract class.

Ask Question:

I asked this question on StackOverflow  Because StackOverflow  is the place to get a well defined answer from PHP experts.
Use StackOverflow if you have any questions. It is better than to ask someone who knows less but they have more attitude to show.

So I asked my question there and instant I got one comment
I wonder even experience developer like Jeremy¬†Harris¬†was unaware of this new feature. although he removed his first comment ūüėČ (Due to reputation)
later on, he posted detail explanation for my query.Screen Shot 2017-05-12 at 10.18.16 PM.png

You can check answer just click on the image.


be¬†smart and stay updated…. ūüďĆ ūüėé

Leave a Comment

Your email address will not be published. Required fields are marked *