7
In PHP I’m used to creating a function and can declare the types of parameters I want, even knowing that the language is weakly typed, but in JS I can do this?
Example in PHP:
<?php
class Teste {
private $names;
/**
* setNames
*
* @param array $name
*
* @return void
*/
public function setNames(array $names) {
$this->names = $names;
}
/**
* getNames
*
* @return array
*/
public function getNames() : array {
return $this->names;
}
}
$nome = new Teste();
$nome->setNames('Lucas');
var_dump($nome->getNames());
Will give the error:
Argument 1 passed to Teste::setNames() must be of the type array, string Given
Excellent explanation!!!!
– Jean Gatto