面向对象的PHP和数据库包装器

问题描述:

我正在用PHP编写一个用于预订系统的小类.我自己承认,OOP根本不是我的事,但我想在项目中使用它,前提是我可以收集到足够的知识以正确使用它.

I'm writing a small class for a booking system in PHP. By my own admission OOP is not my thing at all - but I'd like to use it for this project, assuming I can glean enough of an understanding to use it properly.

这是我到目前为止所拥有的一些东西(大部分是伪造的):

Here's some of what I have so far (mostly pseudo):

class bookingSystemDbWrapper
{
    public $db;
    function __construct()
    {
        $this->db = mysqli_connect(..., ..., ..., ...);    
    }
}

class bookingSystemAvailabilityManager extends bookingSystemDbWrapper
{
    function checkAvailability($date, $roomID)
    {
        /**
         * check availability for a given room on a given date 
         */

        $query = mysqli_real_escape_string("SELECT SOMETHING FROM SOMEWHERE");
        $result = mysqli_query($this->db, $query);
        return $result; 
    }        
}

这种方法对上面的小型和简单类都适用.但是,当我需要一个用于创建预订,基于位置搜索等的类时,为每个类扩展数据库包装器似乎是一个坏主意.因此,我需要让我的数据库包装程序类对其他类可用的另一种方法,以便它们可以运行数据库查询.

This approach works fine for the small and simple classes above. But when I need a class for creating a booking, searching based on location, etc, it seems like a bad idea to be extending the database wrapper for every class. So I need another way to make my database wrapper class available to my other classes, so they can run database queries.

我在这里阅读了有关nettuts的依赖项注入: http://net.tutsplus.com/tutorials/php/dependency-injection-huh/,但这超出了我对OOP的有限理解,因此我不确定采用哪种方法或如何正确实现它.

I've read up on dependency injection on nettuts here: http://net.tutsplus.com/tutorials/php/dependency-injection-huh/ but it's a bit beyond my limited understanding of OOP, so I'm unsure of what approach to take, or how to implement it correctly.

TL; DR

如何在不破坏OOP原理的情况下使一个类的公共变量可供其他类使用,并使我的代码具有可读性?

How should I make one classes' public vars available to other classes without breaking any principles of OOP, and keep my code readable?

我建议使用静态类,如下所示:

I suggest a static class, like this:

<?php
class dbms
    {
        const HOST     = 'localhost';
        const LOGIN    = 'login';
        const PASSWORD = 'myB!GevU1PwD';
        const SCHEMA   = 'mydb';
        const CHARSET  = 'utf8';
        const PORT     = 3306;

        protected static $instance;

        public static function connect()
            {
                if(is_object(static::$instance) && (static::$instance instanceof mysqli))
                    { return static::$instance->ping(); }

                static::$instance = new mysqli(static::HOST, static::LOGIN, static::PASSWORD, static::SCHEMA, static::PORT);

                if(static::$instance->connect_errno)return false;

                static::set_charset(static::CHARSET);
            }

        public static function disconnect()
            {
                static::$instance->close();

                static::$instance = null;
            }

        public static function &getInstance(){ return static::$instance; }
    }
?>

应该易于扩展,并且可以在您需要的任何地方看到

Should be easy-extendable, and seen everywhere you need:

  1. 静态实例只有一个共享连接;
  2. 通过 extend 可以很容易地重新利用它,而不会降低性能;
  3. 它是一个静态类.您可以从任何需要的地方访问它.
  1. There is only one shared connection for static instance;
  2. It might be easy repurposed via extend without loosing performance;
  3. It accessible from everywhere you need, as it's a static class.