Generating Objects help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marty pain
Forum Contributor
Posts: 105
Joined: Thu Jun 11, 2009 5:32 am
Location: Essex

Generating Objects help!

Post by marty pain »

I'm building a simple database exporter that can export data from a given database to either XML or CSV format. I have separated the logic of the encoding into separate classes (XMLEncoder and CSVEncoder), that implements a common interface, so that more methods can be added later. I then have a main class (Exporter) that handles the rest of the export.

My question is, where do I instantiate an Encoder object? I currently have it wrapped up in the constructor of the main class (Exporter), but I'm not enforcing the interface and it feels a little wrong.

The constructor of my class is below

Code: Select all

public function __construct($tableName, $exportType = 1, $exportDestination = 1, PDO $dbh) {
			if(!empty($tableName)) {
				$this->tableNames[] = $tableName;
			}
			$this->exportType = $exportType;
			$this->exportDest = $exportDestination;
			$this->DB = $dbh;
	
			switch($this->exportType) {
				case(self::XML):
					$this->encoder = new XMLExportEncoder();
					break;
			
				case(self::CSV):
					$this->encoder = new CSVExportEncoder();
					break;
			}		
There isn't enough separated logic to go for a full blown factory pattern in my opinion, but all suggestions welcome!
Post Reply