开发者

including class within another class in php

i have a php page which has some variables regarding database i.e server address, username and password etc.

config.php will include

<?php 
  $dbserver="";
  $username="";
  $password="";
  $database=""; 
?>

i have a class which contains all the functions required for my website. How can i import my php page variables into this class to be used for the database connectivity?

my class

<?php
    class a{
      include("conf开发者_如何学JAVAig.php");
      function db_connect(){
        mysql_connect($dbserver,$username,$password);
      }
    }
?>


usually for this purpose, Constants exist.

But if you want to use variables, all you have to do is to require_once(yourFile), then when you want to use these variables (which are global) inside a method of a class, simply refer to them as global $myVar; (as if you're declaring it). Only need to do this once for each global variable you want to use in the context.

Example:

settings.php:

$conn = 'my connection';

MyClass.php:

class MyClass
{
    function DoSomething()
    {
        require_once('settings.php');
        global $conn;
        doSomethingWith($conn);
    }
}


Update

For a Database class that requires configuration options, the simplest way would be to use the config values as parameters (example 1 of my original answer below).

A more complex, though also more flexible approach would be a Config-Class.

class Config
{
  private $config = array();

  public function __construct(array $config)
  {
    $this->config = $config;
  }

  public function Register($key, $value)
  {
    $this->config[$key] = $value;
  }

  public function Get($key)
  {
    if (!isset($this->config[$key])) return null;
    return $this->config[$key];
  }
}

Your DB class would look something like this:

class Database
{
  private $config = null;

  public function __construct(Config $config)
  {
    $this->config = $config;
  }

  public function Connect()
  {
    do_connect_stuff($this->config->Get('host'), $this->config->Get('user'), .....);
  }
}

File config.php

<?php

$config = new Config(
  array(
    "host" => "localhost",
    "user" => "user",
    ...
  )
);

/*
alternative:
$config = new Config();
$config->Register('host', 'localhost');
$config->Register('user', 'user');
...
*/
?>

File that requires the database:

<?php

$database = new Database($config);
$database->Connect();

?>

As a side hint: Use PDO, it's far better than the old mysql_* functions.


Original Answer

The proper style would be to pass the variables to the functions as parameter or pass them when creating the object. You can also use Init methods to pass the parameters.

Examples:
(Which of the following code you should use depends on what you already have and how your code is designed, the 'cleanest' way would be an object for which you transmit the variables when calling the ProcessAction method)

Assuming that in your script you have a Variable $action which you get from $_GET or some other way.

Using an Object

class Controller
{
  public function ProcessAction($action, $some_other_parameter, $and_yet_another_parameter)
  {
    [...]
  }
}

You then call it with

$action = do_some_stuff_to_get_action();
$controller = new Controller();
$controller->ProcessAction($action, $other_parameter, $second_parameter);

Using a static class

class Controller
{
      public static function ProcessAction($action, $some_other_parameter, $and_yet_another_parameter)
      {
        [...]
      }
}

Called with:

$action = do_some_stuff_to_get_action();
Controller::ProcessAction($action, $other_parameter, $second_parameter);

Passing the parameters before calling the function

Object

class Controller
{
  private $action = "";
  private $some_other_parameter = "";

  public function __construct($action, $some_other_parameter)
  {
    $this->action = $action;
    $this->some_other_parameter = $some_other_parameter;
  }

  public function ProcessAction()
  {
    if ($this->action == 'do_stuff')
    {
      [...]
    }
  }
}

Called with:

$action = do_some_stuff_to_get_action();
$controller = new Controller($action, $other_parameter);
$controller->ProcessAction();

Static methods

class Controller
{
      private static $action = "";
      private static $some_other_parameter = "";

      public static function Init($action, $some_other_parameter)
      {
        self::$action = $action;
        self::$some_other_parameter = $some_other_parameter;
      }

      public static function ProcessAction()
      {
        if (self::$action == 'do_stuff')
        {
          [...]
        }
      }
}

Called with:

$action = do_some_stuff_to_get_action();
Controller::Init($action, $other_parameter);
Controller::ProcessAction();


I used the database configuration in the constructor of the class. I think that was the only solution not including any third page in the scenario.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜