How to validate return of a method like php7 object array?

Asked

Viewed 93 times

-1

So I was able to create a class with a method for verify if the return of one another function is a boolean:

Technically and there would enter the orienteering of you, it works like this:

class Classe {}

$a = new Classe();

$util = new Util();

echo $util->instancia ("Classe", $a) ? "Confirmado!" : "Não é uma instancia dessa classe!";

But I’m not getting it adapt that in:

public function buscarTodos () : array ::{}

I need to get ensure that the return of function buscarTodos be in fact a array of objects of a given class.

In the java, just do:

public function buscarTodos () : array<Pessoas> {}

For example, the part I need to take care of is already taken care of. But in the php is very difficult get that outworking.

Note: I could ensure that in the body of method. But that’s not the goal.

2 answers

2


PHP, unlike Java, has dynamic typing. A variable that is now string can receive an integer and change its type without problems and this, inclusive, is expected from the language. This is possible because a PHP variable is built on the basis of a more complex C structure that stores both the current value and a reference to the type.

That said, for language, when you create a array, it doesn’t matter if it’s a array of numbers, of strings or from various instances. Array is array. Only with the return declaration you will not be able to guarantee that it is a array only of one kind. Doing this makes no sense in PHP and if you need this in your application maybe PHP is the wrong language.

To ensure that all elements of the array be all instances of a given class, you will necessarily need to go through the array and check one by one with the instanceof.

There are third-party tools where you can use the return annotation for comments:

/**
 * @return Classe[]
 */

But this is not the language itself. Even this syntax was already proposed in 2014 and was refused:

  • My gosh! They refused?

  • 1

    Yes, because it makes no sense to have return statement in a dynamic typing language.

  • Well, I think I should have the option available to anyone who wants to use it anyway. Although it doesn’t really make sense for that kind of language but it would be interesting to provide that resource if someone needed it. In this case, the programmer would get the resources he needs. But it’s great. It’s playing forward. Thank you for now! Maybe someone in the community has had some similar idea and I find a solution.

  • 1

    That’s the question: you use a dynamic typing language if it gives you any advantage. If static typing brings benefits, just use another language.

  • 1

    Basically you don’t buy a screwdriver waiting for it to have a saw in case you need to cut something.

  • I get it. Thank you

Show 1 more comment

-1

Maybe ::class can help you, because in the tests here he confirmed the type of class.

I test as follows (Most important is at the end):

<?php
namespace classes\util;

class Util {

    public function __construct() {

    }

    public function instancia($classe, $objeto): bool {
        return $objeto instanceof $classe;
    }

}

class Classe {

}

$a = new Classe();

$util = new Util();

// Ao invés de uma string, passe de fato a referência ao tipo da classe. Repare no método abaixo
echo $util->instancia(Classe::class, $a) ? "Confirmado!" : "Não é uma instancia dessa classe!";
?>
  • 1

    Classe::class is just a syntactic sugar that returns "Classe", but with the namespace solved. In this example, it made no difference, so the answer itself had no basis and this is not related to the problem in question which is to define the return as a array of instances.

Browser other questions tagged

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