HomePHP Page 3 - Handling Cookies and File Data with the Factory Pattern in PHP 5
Creating some data saving classes - PHP
Any PHP developer who has been using the object-oriented paradigm for a while for developing web applications knows that the factory design pattern can be really useful when it comes to creating multiple objects across a specific programming context. However, if you're interested in filling some knowledge gaps that you might have about this handy pattern, then this article is possibly what you're looking for.
In the previous section I showed you the pertinent signatures of two concrete factories, which were originally conceived to work with different contexts, called "string" and "object" respectively. Also, it's worthwhile to notice here that the classes in question were capable of creating objects whose main task was saving different types of data entities, that is string and other objects, to simple text files and cookies.
So, taking into account that it's necessary to define the classes that originate the aforementioned objects to complete the programmatic schema dictated by the factory pattern, now I'd like you to take a look at the following classes. They work with basic string and generic objects.
Here they are:
// define abstract 'DataSaver' class abstract class DataSaver{ abstract public function saveData(); abstract public function getData(); }
// define concrete 'FileStringSaver' class class FileStringSaver extends DataSaver{ private $data='This string will saved to target file.'; public function saveData(){ if(!$fp=fopen('filedata.txt','w')){ throw new Exception('Error opening target file.'); } if(!fwrite($fp,$this->data)){ throw new Exception('Error writing data to target file.'); } fclose($fp); } public function getData(){ return $this->data; } }
// define concrete 'CookieStringSaver' class class CookieStringSaver extends DataSaver{ private $data='This string will be saved to target cookie.'; public function saveData(){ setcookie('default_cookie',$this->data); } public function getData(){ return $this->data; } }
// define concrete 'FileObjectSaver' class class FileObjectSaver extends DataSaver{ private $inputObj; public function saveData(){ if(!$fp=fopen('filedata.txt','w')){ throw new Exception('Error opening target file.'); } if(!fwrite($fp,serialize($this->inputObj))){ throw new Exception('Error writing object data to target file.'); } fclose($fp); } public function getData(){ return $this->inputObj; } public function setInputObj($inputObj){ if(!is_object($inputObj)){ throw new Exception('Input data must be an object.'); } $this->inputObj=$inputObj; } }
// define concrete 'CookieObjectSaver' class class CookieObjectSaver extends DataSaver{ private $inputObj; public function saveData(){ setcookie('default_cookie',serialize($this->inputObj)); } public function getData(){ return $this->inputObj; } public function setInputObj($inputObj){ if(!is_object($inputObj)){ throw new Exception('Input data must be an object.'); } $this->inputObj=$inputObj; } }
As you can see, all the definitions of the above classes are actually very easy to follow, since they were introduced in the beginning of this section. However, let me go one step further and explain in detail how they work, in case you don't grasp the logic that stands behind these classes.
There are two classes that are tasked with saving simple strings to plain text files and cookies, which implies that they should work with a predefined "string" context. Two additional classes are defined subsequently, this time with the purpose of storing generic objects in the same physical locations mentioned previously, meaning that they must be used in an "object" environment.
So far, so good. At this point I hopefully demonstrated how the programmatic model imposed by the factory pattern can be easily implemented with PHP 5. On one hand, there are two concrete factory classes that work with a pair of specified environments, while on the other hand there are four "product" classes that are capable of saving certain data structures to cookies and text files.
In simple terms, the previous scenario describes theoretically the way that the factory pattern works, but it's necessary to complement this theory with a practical example so you can see how all the classes defined earlier can be put to work together.
Therefore, in the section to come I'm going to set up an example to show you more clearly how to use the factory classes that were developed previously.
To see how this working example will be developed, please click on the link below and keep reading.