PHP return methods guidelines

What are and why are the best practices for this case, in PHP:

class Main { public function getSomeBean() { ... return array( 'value1' => $value1, 'value2' => $value2 ); } } 

or

 class SomeBean() { $value1; $value2; } class Main { public function getSomeBean() { $someBean = new SomeBean(); ... return $someBean; } } 

In java, it is best to use the bean class. But in PHP, I always see the returned array, but in Netbeans it’s hard to know what the keys to the array are, only reading documents with the class is simpler, but on the other hand, it gives more work. So ... What is the best way?

+1
source share
1 answer

Depends on what you return. If it makes sense to have a container class (pretty much like C structs), you can go for it. If you return the same data type, you should go instead of arrays.

But creating a class only for the old specific return type does not make sense. Either you create a complete class with the correct constructor and the correct methods, or just use stdClass .

At this point, using stdClass or array as a container is not a big deal. There is no right or wrong. PDO, for example, provides both because it is up to you and your style.

0
source

Source: https://habr.com/ru/post/987268/


All Articles