views:

25

answers:

2

I write this code

include('database.php');

function get_something() {

database instructions

}

function get_another(){

database instructions

}

and I try to fix by this

function get_something() {
  include('database.php');
  database instructions

}

function get_another(){
  include('database.php');
  database instructions

}

I get redeclaration error .

how can I fix this ?

Thanks

+2  A: 

Either include all of your files at a point of your application that is guaranteed to be only executed once, or use include_once 'database.php';

Read up on it here. Alternatively, you could implement autoloading. PHP will then load classes if, and only if, it needs them. It doesn't work for global functions (since they aren't classes), though. You'd have to wrap them in a class if you want to take advantage of this.

ryeguy
A: 

Seperate configuration of the database and the functions in different files. Include the file with the functions first, only once (require_once is nice for this).

Then the database config where needed. This can be saved as returning an array for example

<?php
return array(
    'db1' => array(
        'user' => 'sdf',
    ),
);

and

$config = include 'config.php';

Thats the "quick fix now" method. But you really should use OOP, and autoload.

OIS