In recent reading, I see conflicting recommendations on encapsulation methods and OOP best practices.
I am beginning development of a series of PHP classes that will be used to transport and transform data from multiple source systems to a final destination. Therefore, the properties of the first class are to contain source URL and authentication values.
Which of the following is best for a long-term project with unlimited potential for expansion?
-
Declare as public properties. Set values externally for each source when constructing class. Pro: simple. Con: No encapsulation advantage
-
Use __get and __set. Set values externally for each source. Pro: Follows OOP convention. Con: Opens all to external access; again, no encapsulation
-
Declare properties as protected. For each source system I need to work with, extend the original class and set the properties in the subclass. Pro:OOP with encapsulation. Con: more classes, and potentially files, to manage.
Currently, option 3 seems the best, despite the file overhead. I’m also open to other ideas.
References I have read for this question:
http://typicalprogrammer.com/?p=23
http://www.php.net/manual/en/language.oop5.overloading.php
Independent getter/setter methods, or combined?
There is at least one more option: inject these parameters into the objects on construction and make them read-only “properties” through a getter. Construct objects only through a factory (you can possibly enforce this as well but I ‘m not sure if there’s any tangible benefit in doing so).
The factory can be configured on startup (which could be a plus), there is only one class of transport, and consumers can only view each transport’s state in the manner it chooses to expose it (encapsulation).