Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hinting in class variables

<?php  namespace Sandbox;  class Sandbox {      private Connectors\ISandboxConnector $connection;      public function __construct(Connectors\ISandboxConnector $conn) {         $this->connection = $conn;     }  }  ?> 

For the above code I'm getting the following error:

Parse error: syntax error, unexpected 'Connectors' (T_STRING), expecting variable (T_VARIABLE) 

When I remove the type hinting and var_dump that $connection variable, it will be private Sandbox\Sandbox and not Sandbox\Connectors\ISandboxconnector, why?

like image 794
Gerben Jacobs Avatar asked Sep 23 '13 10:09

Gerben Jacobs


People also ask

Which data types can be declared with type hinting?

With Type hinting we can specify the expected data type (arrays, objects, interface, etc.) for an argument in a function declaration. This practice can be most advantageous because it results in better code organization and improved error messages.

What is a type hint?

PEP 484 introduced type hints — a way to make Python feel statically typed. While type hints can help structure your projects better, they are just that — hints — and by default do not affect the runtime.

Should I use type hinting in PHP?

Apparently 'type hinting' in PHP can be defined as follows: "Type hinting" forces you to only pass objects of a particular type. This prevents you from passing incompatible values, and creates a standard if you're working with a team etc.

How do you write a class hint in Python?

In a type hint, if we specify a type (class), then we mark the variable as containing an instance of that type. To specify that a variable instead contains a type, we need to use type[Cls] (or the old syntax typing. Type ).


2 Answers

PHP 7.3 and below does not support typed properties. You could only define a variable as below:

class Sandbox {     private $connection; 

However, to help editors understand your code, you may use a @var tag to document the expected type of the property:

class Sandbox {     /** @var Connectors\ISandboxConnector */     private $connection; 

Update

PHP 7.4.0

Thanks @Manuel for mentioning the new update, PHP 7.4 now introduces typed properties according to PHP RFC: Typed Properties 2.0.

Property type declarations support all type declarations supported by PHP, with the exception of void and callable. Any class or interface name, stdClass, scalar and compound types, references to parent and own objects are also supported.

class Sandbox {     public int $id;     public string $name;     private Connectors\ISandboxConnector $connection; } 

Note: keep an eye on side effects such as uninitialised state and inheritance strict rules.

like image 78
Vahid Hallaji Avatar answered Oct 15 '22 15:10

Vahid Hallaji


Since PHP 7.4 you are able to type hint on class properties, as shown here. So for example your $connection property would look like this:

class Sandbox {     private Connectors\ISandboxConnector $connection; } 
like image 25
Manuel Avatar answered Oct 15 '22 15:10

Manuel