Assign type to array in PHP

7

I know that PHP is a poorly typed language. But is there any alternative way to "type" a array in PHP?

Example, I can do this below to force a given data type to be given in a function.

function exemplo(Classe $valor){
    // meu código aqui ...
}

However, I would like to know how to do something like this in Java (see below) with lists.

List<String> array = new ArrayList<>;

Is there a way to do this in PHP?

    
asked by anonymous 04.02.2015 / 19:54

3 answers

6

Not possible.

PHP is dynamically and poorly typed. In the background nothing has a fixed type. All arrays of PHP accept that any element has any type. Can mix at will. It is an inherent characteristic of this type of language. There is no syntax or option in the compiler that can force or even indicate what kind of data the elements can have. That is, they will always be mixed according to the PHP definition.

Now Hack which is an evolution of PHP created by Facebook allows generics and " arrays" can be typed. Although it is based on PHP, it is another language.

    
04.02.2015 / 20:02
1

There is actually a way to type php though it is dynamically typed. Take a look at this link link

To type php you should do this:

settype($foo, "integer");

Assuming that $ foo is an integer type. As the friend mentioned above there is a hack language that works with a typed "php", I have already done some tests in the language, but I do not think it is mature enough to abandon the good old php.

    
06.02.2015 / 04:41
1

This is also something that has always bothered me not to know the types of elements I have inside an array.

The implementation I decided to extend was to the IteratorIterator class of PHP. This class requests that an Iterator interface be injected and so it uses its methods by the magic __ call method. What was done was to create an input method for the elements with the typed parameter and insert another input method of the ArrayIterator (offsetSet) class as private.

So I made this code.

  
class TestArrayIterator extends IteratorIterator {

    private $arrayIterator;

    public function __construct() {
        $this->arrayIterator = new ArrayIterator();
        parent::__construct($this->arrayIterator);
    }

    public function append(IElement $element) {
        $this->offsetSet(null, $element);
    }

    private function offsetSet($key, IElement $element) {
        $this->arrayIterator->offsetSet($key, $element);
    }

}

interface IElement {

    public function getCodigo();
    public function getNome();

}

class Element implements IElement {

    private $codigo;
    private $nome;

    public function setCodigo($codigo) {
        $this->codigo = $codigo;
    }

    public function setNome($nome) {
        $this->nome = $nome;
    }

    public function getCodigo() {
        return $this->codigo;
    }

    public function getNome() {
        return $this->nome;
    }
}

$element = new Element();
$element->setCodigo(1);
$element->setNome('nome 1');

$element_1 = new Element();
$element_1->setCodigo(2);
$element_1->setNome('nome 2');


$testIterator = new TestArrayIterator();
$testIterator->append($element);
$testIterator->append($element_1);

foreach ($testIterator as $key => $element) {   
    echo $key . ' - ' . $element->getCodigo() . ' => ' . $element->getNome() . "\n";    
}
 
    
13.11.2016 / 04:59