Home > Backend Development > PHP Tutorial > php try catch: catch exception, throw exception

php try catch: catch exception, throw exception

PHP中文网
Release: 2016-07-29 09:01:03
Original
2152 people have browsed it

php try catch: catch exceptions, throw exceptions:

/**     
* 构造函数:自动加载连接数据库     
* @param  $database  $key     
* @return       
*/    
private function __construct($database = array(), $key){
        try{
            $this->server = $database['db_host'];            
            $this->username = $database['db_user'];            
            $this->password = $database['db_pwd'];            
            $this->database_name = $database['db_name'];            
            $this->database_type = $database['db_type'];            
            $this->database_port = $database['db_port'];            
            if($this->database_type == 'mysql'){                
            $this->pdo = new PDO($this->database_type . ':host=' . 
            $this->server . ';port=' . 
            $this->database_port                     . ';dbname=' . 
            $this->database_name, 
            $this->username,
            $this->password, array(PDO::ATTR_TIMEOUT => self::TIME_OUT));            
            }else{            
            $this->pdo = new PDO ("dblib:host=$this->server:$this->database_port;
            dbname=$this->database_name","$this->username","$this->password");            
            }            
            $this->pdo->exec('SET NAMES \'' . $this->charset . '\'');            
            self::$pdo_ref[$key] = &$this->pdo;                       
            }catch(PDOException $e){            
            echo $e->getMessage();        
            }    
            }
Copy after login

The above introduces php try catch: catch exceptions, throw exceptions, including aspects. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template