如果声明无法按预期工作

Look at lines #111 and #116

<?php
class Connection_model extends CI_Model {
    private $validated;
    private $sender_db;
    private $sender_host;
    private $sender_user;
    private $sender_pw;
    private $receiver_db;
    private $receiver_host;
    private $receiver_user;
    private $receiver_pw;
    public $err_sender;
    public $err_receiver;


    private function define_database($target) {
        if (in_array('sender', $target)) {
            $db['sender'] = array(
                    'dsn'   => '',
                    'hostname' => $this->sender_host,
                    'username' => $this->sender_user,
                    'password' => $this->sender_pw,
                    'database' => $this->sender_db,
                    'dbdriver' => 'mysqli',
                    'dbprefix' => '',
                    'pconnect' => FALSE,
                    'db_debug' => TRUE,
                    'cache_on' => FALSE,
                    'cachedir' => '',
                    'char_set' => 'utf8',
                    'dbcollat' => 'utf8_general_ci',
                    'swap_pre' => '',
                    'autoinit' => TRUE,
                    'encrypt' => FALSE,
                    'compress' => FALSE,
                    'stricton' => FALSE,
                    'failover' => array(),
                    'save_queries' => TRUE
            );
            return $db['sender'];
        }
        elseif (in_array('receiver', $target)) {
            $db['receiver'] = array(
                    'dsn'   => '',
                    'hostname' => $this->receiver_host,
                    'username' => $this->receiver_user,
                    'password' => $this->receiver_pw,
                    'database' => $this->receiver_db,
                    'dbdriver' => 'mysqli',
                    'dbprefix' => '',
                    'pconnect' => FALSE,
                    'db_debug' => TRUE,
                    'cache_on' => FALSE,
                    'cachedir' => '',
                    'char_set' => 'utf8',
                    'dbcollat' => 'utf8_general_ci',
                    'swap_pre' => '',
                    'autoinit' => TRUE,
                    'encrypt' => FALSE,
                    'compress' => FALSE,
                    'stricton' => FALSE,
                    'failover' => array(),
                    'save_queries' => TRUE
            );
            return $db['receiver'];
        }
    }


    // Validate the connection(s)
    private function validate($target) {
        /* @param: Can be 'sender' (string), 'receiver' (string) or 'sender' and 'receiver' (array) */ 
        // Go through all parameters and define an array            
        if ($target == 'sender' || in_array('sender', $target)) {
            $sessions = array('connection', 'sender_db', 'sender_host', 'sender_user', 'sender_pw');
        }
        elseif ($target == 'receiver' || in_array('receiver', $target)) {
            $sessions = array('connection', 'receiver_db', 'receiver_host', 'receiver_user', 'receiver_pw');
        }
        else {
            echo 'Error: illegal parameter. Please use sender or receiver instead.';
        }

        // Check if all keys from the array are saved in session
        if (isset($sessions)) : 
            foreach ($sessions as $value) {
                if (key_exists($value, $this->session->get_userdata())) {                   
                    $this->validated = true;
                }
            }   
        endif;                  
    }


    // Establish one or many connections
    /* @param: Can be 'sender' (string), 'receiver' (string) or 'sender' and 'receiver' (array) */
    public function establish($target) {
        if ($target == 'receiver' || in_array('receiver', $target)) { $receiver = 1; }
        elseif ($target == 'sender'  || in_array('sender', $target)) { $sender = 1; }
        if ($sender = 1 || $receiver == 1) {    
            $db_values = array();   
            $this->validate($target);
            if ($this->validated) {         
                // Aight, let's go ahead and connect this baby              
                if ($sender == 1) {
                    array_push($db_values, 'sender');
                    $this->sender_db = $this->session->userdata('sender_db');
                    $this->sender_host = $this->session->userdata('sender_host');
                    $this->sender_user = $this->session->userdata('sender_user');
                    $this->sender_pw = $this->session->userdata('sender_pw');                   
                    if ($this->load->database($this->define_database($db_values))) {
                        $this->err_sender = 0;
                        return $this->load->database($this->define_database($db_values), TRUE);
                    }   
                    else {
                        echo '<br>AHHHHHHHHHHHHHHHHH<br>';
                        $this->err_sender = 1;
                        $this->session->unset_userdata('connection');
                    }
                }
                elseif ($receiver == 1) {
                    array_push($db_values, 'receiver');
                    $this->receiver_db = $this->session->userdata('receiver_db');
                    $this->receiver_host = $this->session->userdata('receiver_host');
                    $this->receiver_user = $this->session->userdata('receiver_user');
                    $this->receiver_pw = $this->session->userdata('receiver_pw');
                    if ($this->load->database($this->define_database($db_values))) {                        
                        $this->err_receiver = 0;
                        return $this->load->database($this->define_database($db_values), TRUE);                     
                    }
                    else {                      
                        $this->err_receiver = 1;
                        $this->session->unset_userdata('connection');
                    }                                           
                }   
                else {
                    echo 'Error: illegal parameter. Please use sender or receiver instead.';
                }   
                if ($this->err_receiver == 1 || $this->err_sender == 1) {                   
                    // redirect('home');
                    exit;
                }
            }
            else {
                echo 'Oops, there is an error! For some reason the property "validated" is not returning true (Connection_model.php)';
                exit;
            }
        }
        else {
            echo 'Error: illegal parameter. Please use sender or receiver instead.';
        }
    }

}

I ALWAYS get the echo 'AHHHHHHHHHHHHHHHHH'. This is probably because if ($this->load->database($this->define_database($db_values))) returns false. But why does it return false? It should only return false when the database connection could not be established.

The first problem I see, is in your method establish(), you have a syntax error in your if conditional, as you may already know = assigns what the variable means, and == compares two variables. Change the following:

you have:

 if ($sender = 1 || $receiver == 1) {

it should be :

 if ($sender == 1 || $receiver == 1) {

Second problem I see, when you check for the keys in session. According to your comment, you're iterating through the sessions array to check if ALL keys are saved, if one is set then it will set to true, then check the next, but what if one is not set youre not setting it to false. Fix it like this:

if (isset($sessions)):
    foreach ($sessions as $value) {
        if (key_exists($value, $this->session->get_userdata())) {
            $this->validated = true;
        } else {
            $this->validated = false;
            break;
        }
    }
endif;

Third problem, when you're checking if your database has loaded, your if statement is not checking for a bool therefore always giving you the same result. To receive a bool, you have to first change your database db_debug to FALSE, then try to initialiaze the db and check for results like this:

private function define_database($target) {
    if (in_array('sender', $target)) {
        $db['sender'] = array(
            'dsn' => '',
            'hostname' => $this->sender_host,
            'username' => $this->sender_user,
            'password' => $this->sender_pw,
            'database' => $this->sender_db,
            'dbdriver' => 'mysqli',
            'dbprefix' => '',
            'pconnect' => FALSE,
            'db_debug' => FALSE, //SET THIS TO FALSE
            'cache_on' => FALSE,
            'cachedir' => '',
            'char_set' => 'utf8',
            'dbcollat' => 'utf8_general_ci',
            'swap_pre' => '',
            'autoinit' => TRUE,
            'encrypt' => FALSE,
            'compress' => FALSE,
            'stricton' => FALSE,
            'failover' => array(),
            'save_queries' => TRUE,
        );
        return $db['sender'];
    } //....your other code
}

Now in establish():

$db_obj = $this->load->database($this->define_database($db_values));
if ($db_obj->initialize()) {
    $this->err_sender = 0;
    return $db_obj;
} else {
    echo '<br>AHHHHHHHHHHHHHHHHH<br>';
    $this->err_sender = 1;
    $this->session->unset_userdata('connection');
}

change this line

if ($sender = 1 || $receiver == 1) {

to this line

if ($sender == 1 || $receiver == 1) {