phpphp-8.2

php class constuctor text before property


i trie to figure out what the use of the "text" is before te propery/variable in the contructor. What is the use of "TableGatewayInterface" in the constructor in the example below?

use Laminas\Db\TableGateway\TableGatewayInterface;

private function __construct(TableGatewayInterface $tableGateway)
{
     $this->tableGateway = $tableGateway;
}

Solution

  • It states that $tableGateway property must come from TableGatewayInterface, must be declared in that aliased struct. It means that You cannot simply make an instance with such constructor, with parameter of any value of Your choice, but with named parameter strictly from TableGatewayInterface.

    Here is nice insight for You: List of Types Supported By PHP

    Research PHP named parameters and return type declarations.