0

In the pages so far, I have had used one connection class and one class to hold the logic for the page. However, I now have a page where I need to use multiple classes based on different scenarios that may happen.

This is what I have been able to make work, but it seems like bad practice. I've searched around but can't find a solution. So my question is, what is a better way of what I'm trying to do.

<?php
require 'class/connect.php';
$db = new Connect();
require 'class/normal-page.php';
require 'class/get-page.php';
?>

    <?php if (!isset($_GET['id'])) { ?>
        <?php $normalpage = new NormalPage($db); ?>
    <?php
        } else {
        $id = $_GET["id"];
        $getpage = new Getpage($id, $db);
    ?>

        <h1><?php echo $getpage->getTitle(); ?></h1>
        <p><?php echo $getpage->getDescription(); ?></p>
    <?php } ?>


class Normalpage {

    function __construct($db) {

        $query = 'SELECT something FROM table';
        $result = $db->query($query);
}
}

Hope that's enough info to make sense. So in short, I include the connect class and use the variable in both other classes.

Alex L
  • 641
  • 7
  • 22
  • 1
    In addition to @nickb's answer, I would recommend you look into autoloading of classes. That way you don't have to specify which class files you have to include, as they are loaded automatically, see http://php.net/manual/en/language.oop5.autoload.php – jeroen Jun 23 '12 at 13:32
  • I think you can make your life a little bit easier with [autoloading](http://php.net/manual/de/language.oop5.autoload.php) as well. **Edit:** Like jeroen wrote, same time, same idea ;) – hakre Jun 23 '12 at 13:33

1 Answers1

3

This is a proper way of dealing with the database object. In fact, it is called the dependency injection design pattern, and is actually quite common. You might want to consider saving the constructor parameter $db to a member variable so other class methods may access the database via something like $this->db if you declare the member variable as private $db;.

However, you might want to consider the factory design pattern, as it will help organize the classes for your pages. See this example from the above link:

class Factory
{
    public static function build($type)
    {
        $class = 'Format' . $type;
        if (!class_exists($class)) {
            throw new Exception('Missing format class.');
        }
        return new $class;
    }
}

class FormatString {}
class FormatNumber {}

try {
    $string = Factory::build('String');
}
catch (Exception $e) {
    echo $e->getMessage();
}

try {
    $number = Factory::build('Number');
}
catch (Exception $e) {
    echo $e->getMessage();
}
nickb
  • 56,839
  • 11
  • 91
  • 130
  • 1
    Also, you could have a look at making Connect a [singleton](http://en.wikipedia.org/wiki/Singleton_pattern) or just replacing `$db` with a static class variable on `Connect`. This would mean you could always access your $db variable as `Connect::db` or `Connect::db()`. – Stecman Jun 23 '12 at 13:22
  • You'll run into trouble with singletons when having to deal with multiple DB connections at the same time. – brezanac Jun 23 '12 at 13:25