Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - use of bareword in interface function declarations

Looking at PHP's documentation about interfaces, specifically here: PHP: Object Interfaces - Manual. The following code is given as a working example. Could someone explain what the bareword 'Baz' being declared as part of the function signature is please?

<?php
interface a
{
    public function foo();
}

interface b extends a
{
    public function baz(Baz $baz);
}

// This will work
class c implements b
{
    public function foo()
    {
    }

    public function baz(Baz $baz)
    {
    }
}
like image 414
dewd Avatar asked Dec 06 '25 14:12

dewd


1 Answers

It is called type hinting.

The baz() method expects the first argument, $baz, to be an object of the type Baz. An object's type comes from either the class that it is built from, or from an interface that it implements.

like image 174
Sverri M. Olsen Avatar answered Dec 09 '25 19:12

Sverri M. Olsen