Can i in some way declare an array value to be a string, int etc. without adding a value to it?
For example:
public $arr = array(string);
Can i in some way declare an array value to be a string, int etc. without adding a value to it?
For example:
public $arr = array(string);
Short answer, no. PHP is a weakly typed language and doesn't impose types. Closest thing are type hints, but those won't help you in this case.
PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used.
from http://php.net/manual/en/language.types.type-juggling.php
it's just thought, maybe incorrect but you can test
public function login()
{
$this->index();//not done yet
$arr = array();
foreach ($arr as $element)
{
$element = '';
// or $element = (string)$element;
}
}
Maybe better way will be to use object of stdClass instead of array; or array of stdClass objects.