Assign array type in PHP

Asked

Viewed 823 times

7

I know PHP is a weakly typed language. But there is some alternative way to "type" a array in PHP?

Example, I can do this below to force receive a specific type of data in a function.

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

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

List<String> array = new ArrayList<>;

There is a way to do this in PHP?

  • You want to ensure that within the array there are only values of a specific type?

  • Yes. By default PHP treats them as Mixed. Queri to set an X type. @Cahe

  • This is just to improve the IDE’s auto-complete or have something special in your code that you need.

  • 1

    Actually it’s just mania why I use it in java.

  • 3

    I wouldn’t say it’s all mania, it makes sense but language won’t allow it.

  • @bigown in his opinion this is a weak point of the language or simply a characteristic inherent to PHP that it is up to each one their respective choice of language for their project?

  • 1

    It is well opinion. I think it is a weak point and a strong point :) Nothing is all good and all bad. In programming we always talk about tradeoffs. We always trade one thing for another. We make choices of which defect we want to live with since nothing is perfect. For large code bases manipulated by large teams in systems that are made to last having no types can make it very difficult. But it also simplifies codes in several situations. It’s getting long, it would even give a good question (asked the right way). If nobody posts anything in the next few days, I do.

  • Thank you again and I’m looking forward to watching the conversation unfold. @bigown

  • 1

    Discussion may not, only objective answers (or by the least subjective good) :)

  • It sounded tin, but I expressed it wrong. I didn’t mean DISCUSSION in the aggressive sense. In fact, I talked about debating the issue and each presenting his or her point of view.

  • 1

    And I just joked, I had understood :) But even debate is not allowed nests website.

Show 6 more comments

3 answers

6


Is not possible.

PHP was dynamically and weakly typed, in the background nothing has fixed type. All arrays PHP accepts that any element has any type. It can be mixed at will. It is an inherent feature 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.

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

It is possible partially in PHP since version 7, and in 7.4 this becomes stronger, but it is done in a bad way because it has the legacy without type.

  • "Not possible" natively :)

  • How is it possible?

  • With some class that implements Arrayaccess to simulate an Array. I thought Spltype did this, but it only fits the other values (other than Array)

  • 1

    And how the compiler would verify this?

  • Thank you @bigown

  • 1

    There’s no way, @bigown. In PHP, for these things to work (and it’s not even 100%) just install extensions or else simulating a typed array at the base of the gambiarra

Show 1 more comment

1

Actually there is a way to type php even though it is dynamically typed. Take a look at this link http://php.net/manual/en/function.settype.php

To type php you must do so:

settype($foo, "integer");

So assuming $foo is the whole kind. As the friend above mentioned there is the hack language that works with a typed "php", I have performed some tests in the language, but I do not think still mature enough to abandon the good old php.

1

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

The implementation I decided to do was to extend the Class Iteratoriterator PHP. This class asks for an Iterator interface to be injected and so it uses its methods by the magic method __call. What was done was to create an input method for the elements with the typed parameter and insert another Arrayiterator class input method (offsetSet) 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";    
}
 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.