Jump to content

[SOLVED] Sub Classes or Extensions??


eddedwards

Recommended Posts

Im wondering what the main differences or limitations there are between the following examples. I think there just 2 ways of doing the same thing but I'm not sure. Should I be using extensions or subclass' or does it even matter.

 

Im currently using the example 1 method and in the same sort of program.


<?php
// example 1

class DB_Handler
{
var $subclass, $connection, $result;

function __construct($db_type)
{
	switch ($db_type)
	{
		case "mysql":
			$this->subclass = new mysql_database();
		break;

		case "sqlite":
			$this->subclass = new sqlite_database();
		break;
	}
}

function query($args)
{
	$this->subclass->query($args);
}

}

$instance = new DB_Handler("sqlite");
$instance->query('foobar');
echo $instance->result;



// ****************************************************
// example 2

class DB_Handler
{
var $connection, $result
}

class mysql_database extends DB_Handler
{
function query()
{
	// do query stuff
}
}

$instance = new mysql_database();
$instance->query('foobar');
echo $instance->result;

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.