1

Which 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 is a best practice use always a bean class. But in PHP i always see return array, but in Netbeans is hard to know what is the keys of array, only reading the docs, with a class is more easy, but on the other hand, gives more work. So... what is the best way?

Bruno Nardini
  • 461
  • 5
  • 13
  • 1
    `"i always see return array"` is not really a good indication of what is right or wrong. Many people in the PHP world are self taught and as such may very possibly not be in tune with the "best ways". IMO it all depends on what you are wanting to do with the returned values. – Lix Feb 19 '13 at 22:25
  • Read this: http://stackoverflow.com/q/6710967/434171 – Marcos Feb 19 '13 at 22:28
  • If you expect a array with set amount of key values go with option 1, its silly creating a class just to hold return values, unless this class is going to be injected into other classes. e.g registry pattern. **Tho I would pick nether, and just set object properties.** – Lawrence Cherone Feb 19 '13 at 22:28
  • `"In java is a best practice use always a bean class."` This is relative. Depends on point of view and the reason to use it. – Maykonn Feb 19 '13 at 22:56

1 Answers1

0

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

But creating a class just to old a specific type of return values doesn't make sense. Either you create a complete class with a proper constructor and proper methods or you just use stdClass.

At that point using stdClass or array as a container doesn't really matter much. There's no right or wrong there. PDO for example provides both, because it's up to you and your style.

Shoe
  • 74,840
  • 36
  • 166
  • 272