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
https://forums.phpfreaks.com/topic/78953-solved-sub-classes-or-extensions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.