¿Es este tipo de datos o var constante ZF2?

public function setAlbumTable(AlbumTable $albumTable)
$this->albumTable = $albumTable;
return $this;

I am talking about first parameter ( it's not parameter btw) looks like datatype ? what is it ? constant ? I encounter this when trying to develop app in zend framework 2

preguntado el 10 de marzo de 12 a las 12:03

3 Respuestas

Es PHP's type hinting. It means that the first parameter to this function - $albumTable - must be an instance of the AlbumTable class or a class that inherits from it.

class Car {}

class BlueCar extends Car {}

function drive_the_car(Car $car) {}

drive_the_car(42); // error, first parameter must be an instance of Car

drive_the_car(new stdClass()); // error

drive_the_car(new Car()); // works

drive_the_car(new BlueCar()); // works

respondido 10 mar '12, 13:03

The piece of code you're showing is an example of dependency injection via setter method. The setter is passed an instance of AlbumTable and assignes this instance to a class field.

Your method is passed ONLY ONE parameter: $albumTable.

El AlbumTable before the parameter is a sugerencia de tipo and makes sure that only a instance of AlbumTable or a deriving class can be passed to the setter.

respondido 10 mar '12, 13:03

It forces the actual parameter ($albumTable) to be an instance of AlbumTable class. PHP will give a fatal error if anything else is passed to the function.

This is useful so you don't have to check what type of variable/object you received in order to make use of it's functions and properties.

respondido 10 mar '12, 13:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.