我是否需要在每个文件中包含我的PHP类

I have a class for managing a database. In this class I define the connection and I have various functions for different kind of fetches/retrieving of data.

I usually have a php file for ajax operations and through that I include the files with the functions I need and I pass as a variable to these functions the instance of my class.

Then I use that instance in the functions to make queries and fetch data from the database.

My question is: do I need to include the class declaration in the file with the functions? The structure is as the following... more or less. Basically do I need to include the databaseClass.php in the functionCollection.php

databaseClass.php
<?php

class MyDBManager
{
    private $database   = 'db';
    private $host       = 'host'; 
    private $username   = 'siteguest';
    private $password   = 'psw';
    private $options    = array();
    ...
    function __construct(){}

    function runQuery($statement){}

    function fetchRow(){}
    ...
}
?>

livesearch.php
<?php
    include_once("databaseClass.php");
    include_once("something.php");

    $var1 = $_POST['id'];
    $var2 = $_POST['date'];
    $var3 = $_POST['time'];

    $database = new MyDBManager();

    somefunction($database, $var1, $var2, $var3);
    ...
?>

functionCollection.php
<?php
    include_once("databaseClass.php");
    include_once("misc.php");

    function nameExists($database, $var1, $var2, $var2){}
    ...
?>

No, the file containing the class definition must be loaded exactly once, anywhere. Once the file has been loaded and the class definition has been parsed it is available from anywhere.

PHP Autoloading is designed to prevent this exact problem. When you try to instantiate a class PHP will try to autoload that class before throwing an exception.

Before autoloading we would have to include all our class files. From the PHP documentation:

In PHP 5, this is no longer necessary. You may define an __autoload() function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

However, there's a better option spl_autoload_register allows you to register multiple autoloaders which will be executed one by one until the class is not found and then an exception will be thrown.

There is now a standard in PHP called PSR-0 Standard and came along with PHPs support of namespaces.

You should read this article, it gives a breif history and explains how to implement your own autoloader. http://phpmaster.com/autoloading-and-the-psr-0-standard/

If you get time have a look at some real world autoloaders to learn more, for example Yii or Symfony's framework autoloader.