Longman\TelegramBot\DB::initialize PHP Method

initialize() public static method

Initialize
public static initialize ( array $credentials, Telegram $telegram, string $table_prefix = null, string $encoding = 'utf8mb4' ) : PD\PDO
$credentials array Database connection details
$telegram Telegram Telegram object to connect with this object
$table_prefix string Table prefix
$encoding string Database character encoding
return PD\PDO PDO database object
    public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null, $encoding = 'utf8mb4')
    {
        if (empty($credentials)) {
            throw new TelegramException('MySQL credentials not provided!');
        }
        $dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
        $options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $encoding];
        try {
            $pdo = new PDO($dsn, $credentials['user'], $credentials['password'], $options);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
        self::$pdo = $pdo;
        self::$telegram = $telegram;
        self::$mysql_credentials = $credentials;
        self::$table_prefix = $table_prefix;
        self::defineTables();
        return self::$pdo;
    }

Usage Example

 /**
  * Initialize Database connection
  *
  * @param array  $credential
  * @param string $table_prefix
  *
  * @return Telegram
  */
 public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4')
 {
     $this->pdo = DB::initialize($credential, $this, $table_prefix, $encoding);
     ConversationDB::initializeConversation();
     $this->mysql_enabled = true;
     return $this;
 }
All Usage Examples Of Longman\TelegramBot\DB::initialize