Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of colon (:) operator after member function name in php [duplicate]

Tags:

oop

php

I want to know the meaning of colon after method name i.e.

public function getTitle():Data {

interface Data { 
     public function details(string $name);
}
class Company {
     private $title;

     public function getTitle():Data {
      return $this->title;
     }

     public function setTitle(Data $title)
     {
      $this->title=$title
     }

}

.....
.....
like image 281
user3653474 Avatar asked Apr 10 '18 07:04

user3653474


People also ask

What is colon after Function in PHP?

It indicates the type of value that the function returns, and it's not limited to arrays. For example, you can use float , int or even your own class: class MyClass { } function something(): MyClass { return new MyClass(); }

What does ?: Mean in PHP?

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

What is single colon in PHP?

This (:) operator mostly used in embedded coding of php and html. Using this operator you can avoid use of curly brace.

What's the difference between double colon and arrow in PHP?

There are two distinct ways to access methods in PHP, but what's the difference? sfConfig::set('foo', 'bar'); I'm assuming -> (dash with greater than sign or chevron) is used for functions for variables, and :: (double colons) is used for functions for classes.


1 Answers

public function getTitle():Data {
     return $this->title;
}

"Return type declaration" added since PHP 7.0 (This method should return an object having type "Data").

Like "Argument type declaration", "Return type declaration" is optional.

to check the new features introduced in PHP 7.0

check this link http://php.net/manual/en/migration70.new-features.php

like image 58
T. AKROUT Avatar answered Oct 19 '22 23:10

T. AKROUT