Several resources on my system use the Java collections concept of object collections.
This Collection (abstract) class provides basic functions for another specific class, called lists, that let you find objects through collection indices.
I will use my HTTP header class as an example for explanation.
I have in my constructor an instance of this list class. Each HTTP header field is added to the collection through the Headers :: addHeader () method.
Obviously, I have a getter getHeaders () method that returns a Storage collection, not a Collection Object .
So, if I need to list the headers outside this class, I just need to call $ obj -> getHeaders () , and I have an ArrayObject with all the objects added.
Good!
But recently, it became necessary to use one of the List methods, Lists :: find () , which finds an object that does not even know the name of the object or its specific position in the Storage collection.
Since the List object is in a private property, Headers :: getHeaders () returns the collection store, and I do not want to violate encapsulation by making this property public, I cannot access this method.
Everything that I code, in addition to functionality, should be visually elegant and create another getter method, for example, getHeadersLists () will call, for example:
$obj -> getHeadersLists() -> find( 'foo' );
It's not beautiful!
So, I quickly added __call () to the header class and worked fine:
$obj -> find( 'foo' );
But someone I know (and he is very versed in the topic "Object Orientation") told me that this is wrong.
My argument was focused on readability, and he counter-argued: "In Object Orientation, Magical methods and readability cannot coexist."
So what? What should I do to create this “bridge” between the two classes without using _call () and preserving the principles of object orientation?
I know, I could return the Collection object to Headers :: getHeaders () and use something like:
$obj -> getHeaders() -> find();
But something that I learned about object orientation is a responsibility. The responsibility of this method, as stated in the statement, should return all the headers, not the external object.