Creating the respective sub classes that reside on the lower levels of the mentioned responsibility chain is in fact a straightforward process that can be performed with minor problems. This procedure is limited to deriving a pair of child classes from the "AbstractDataSaver" class shown in the previous section, and coding their respective "getData()" methods in such a way that they can delegate the program flow to the parent class when they can't handle the absence of the mentioned $this->data property by themselves. Of course, you'll understand this concept much better if you take a look at the definition of the new sub class below: // define 'ArraySaver' class After listing the definition of the above "ArraySaver" class, you'll probably know what it does. To put it simply, this new sub class takes up an input array and saves it to a given text file. Simple, right? However, I'd like you to pay careful attention to the signature of all its methods, but more specifically to the following one: public function getData(){ As you can see, the "getData()" method handles the absence of the $this->data property in a slightly different manner than the one shown when the parent "DataSaver" class was defined. More specifically, this method will call the mentioned parent when it is unable to handle this condition on its own. In this way it transfers the responsibility for dealing with the problem in question toward the upper element placed inside the chain. Now do you realize how the a predefined responsibility chain can established across several PHP classes? I bet you do! At this stage, and after understanding properly how the previous sub class works, let's move on to the following section. I will show you how another child class can be created so that we have at least two elements that implement the chain of responsibility that I discussed before. Sounds interesting, doesn't it? Click on the below link and keep reading.
blog comments powered by Disqus |