I read this entire post. It describes what a wrapper class is to other experienced programmers, but not to a newbie like me.
I understand PHP syntax, the object-oriented concept, but have not written my own application in object-oriented code yet. I am beyond confused when trying to figure out what exactly a wrapper class is. I don’t understand the technical jargon.
I hope someone answers with a beautiful detailed child-like description that is easy to understand for someone that understands the basics of object oriented program and has read through pretty much the entire php.net language reference, but has no actual object oriented programming experience. No applications written yet.
Since the explanations on the question you linked to are quite extensive, I will not go an redefine it for you again. Instead, I will try to show you via an example of injection.
So, above you have a class
Loggerthat you may be using to log information somewhere. We don’t really care how it does it, we just know it does.Now, we have a couple of different logging possibilities…
Now, when we use our logger, we do it by doing any of the following:
We always interact with
$loggerin the same way (ie. we callwrite($message)). The wrapper instanceLoggerwraps the actual logging class and calls its methods on our behalf.A more common use of the above type of code would be when using configuration files to determine what your logger is. For example, consider the case where you want to have your logging sent to a file. You might have a config that looks like this:
And your adapted classes might look like:
We would do the same for the other
adaptedclasses. Then, with a little modification to the main WrapperLogger, we could create the correct wrapped instance by using the configuration data and base it on thetypethat is defined in the config. Once you have something like that in place, switching over to logging via email is as easy as changing thetypein the config.