Results 1 to 9 of 9
May 5, 2013, 08:57 #1
- Join Date
- May 2006
- 2 Post(s)
- 0 Thread(s)
Questions about PHP OOP best practice
I'm trying to improve the quality of my code and am using classes/objects a lot more now. It has occurred to me there are a number of things I'm undecided about as to the best way to proceed. I know there's no right or wrong way but would be interested in your thoughts on:
- If your class accepts a number, for example a page number, should you validate the number (intval, (int), is_numeric, etc) and then send it to the class—or should the class do it? My gut feel is the class might validate the page (for example, make sure it is between 1-10) but it should assume that variable is of the right type. This is so that the class represents only one thing (rather then one thing + generic validation)
- How do you handle internal errors in classes? For example, you can't return anything from a constructor. What do you do?
- I read a lot that static functions are a bad idea because it makes unit testing difficult (I don't fully understand this myself yet but I will get to that in due course). However, they seem useful to me in that I can group them together in classes (rather than functions.php) and autoload them via namespace. If you have them as procedural, you cannot do dependency injection and make the functions non-static seems pointless when they don't have any state. Thoughts?
- Given point 3, are const properties also bad for unit testing/bad practice?