2013-04-26 52 views
0

我想在try catch塊內使用屬性。 try catch塊在類內。 我想擴展一個特定的類,使其成爲處理異常的類的子類。 問題是,當我嘗試從子類中使用這些變量時,它總是說未定義。我必須刪除兩個類才能捕獲屬性。通過在try catch塊內部添加一個return語句(我添加了return 1)之後,在閱讀了其他一些答案之後,它似乎不起作用,並且它始終表示未定義的變量。 有什麼幫助嗎?屬性不能在try catch塊外使用

語言是php

的源代碼沒有類作品完美:

try 
    { 
     //$pdo variable to insert PDO object information 
     $pdo = new PDO('mysql:host=localhost;dbname=studenti', 'root', ''); 

     //Set php to catch exceptions 
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     //Set UTF-8 for character encodings 
     $pdo->exec('SET NAMES "utf8"'); 
    } 
    //Catch error if unable to connect 
    catch(PDOException $e) 
    { 
     //error variable 
     $error = 'Unable to connect with database. ' . $e->getMessage(); 

     //include file once and show on screen error message 
     include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/error.inc.php'; 
     //Exit and don't process further 
     exit(); 
    } 

    //Another Exception handling 
    try 
    { 
     //Select statement 
     $sql = 'SELECT * FROM dega'; 
     $select = $pdo->query($sql); 
    } 
    catch(PDOException $e) 
    { 
     //error variable 
     $error = 'Unable to select table. ' . $e->getMessage(); 

     //include file once and show on screen error message 
     include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/error.inc.php'; 

     //Exit and don't process further 
     exit(); 
    } 

的源代碼類不起作用:

<?php 
    //PDO class, connection with MySQL database 
    class Connect 
    { 
     function connection() 
     { 
     $pdo = null; 
      try 
      { 
       //$pdo variable to insert PDO object information 
       $pdo = new PDO('mysql:host=localhost;dbname=studenti', 'root', ''); 

       //Set php to catch exceptions 
       $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

       //Set UTF-8 for character encodings 
       $pdo->exec('SET NAMES "utf8"'); 
      } 
      //Catch error if unable to connect 
      catch(PDOException $e) 
      { 
       //error variable 
       $error = 'Unable to connect with database. ' . $e->getMessage(); 

       //include file once and show on screen error message 
       include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/error.inc.php'; 
       //Exit and don't process further 
       exit(); 
      } 
     } 
    } 

    class Select extends Connect 
    { 
     function selection() 
     { 
      //Another Exception handling 
      try 
      { 
       //Select statement 
       $sql = 'SELECT * FROM dega'; 
       $select = $pdo->query($sql); 
      } 
      catch(PDOException $e) 
      { 
       //error variable 
       $error = 'Unable to select table. ' . $e->getMessage(); 

       //include file once and show on screen error message 
       include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/error.inc.php'; 

       //Exit and don't process further 
       exit(); 
      } 
     } 
    } 

    //Output if successful 
    $error = 'Database connection established.'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/inc/error.inc.php'; 
?> 

回答

0
  1. 你沒有子類。 Object Inheritance
  2. 您沒有物業。 Properties

閱讀關於Classes and Objects


class Connect 
    { 
    protected $pdo = null; 

    public function connection() 
     { 
     $pdo = null; 
     try 
      { 
      $this->pdo = new PDO('mysql:host=localhost;dbname=studenti', 'root', ''); 
      $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
      $this->pdo->exec('SET NAMES "utf8"'); 
      } catch (PDOException $e) 
      { 
      $error = 'Unable to connect with database. ' . $e->getMessage(); 
      include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/error.inc.php'; 
      exit(); 
      } 
     } 
    } 

class Select extends Connect 
    { 

    function selection() 
     { 
     try 
      { 
      $sql = 'SELECT * FROM dega'; 
      $select = $this->pdo->query($sql); 
      } catch (PDOException $e) 
      { 
      $error = 'Unable to select table. ' . $e->getMessage(); 
      include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/error.inc.php'; 
      exit(); 
      } 
     } 
    } 
+0

只是擴展,忘了添加它。 即使聲明瞭$ pdo,它仍然不起作用,它說未定義。我宣佈它爲空。 – GSquadron 2013-04-26 00:38:25

+0

@GSquadron,請閱讀文檔。你可以從那裏獲得所有需要的信息。 – sectus 2013-04-26 00:44:01

+0

您無法在文檔中找到如何調用try catch塊之外的屬性。我在此之前先修改了它。 – GSquadron 2013-04-26 01:01:34

0

$pdo之前connection()

class Connect 
{ 
    protected $pdo = null; // or var $pdo = null; 
    function connection() 
    { 
    ... 

在函數內部它是一個局部變量不是一個類級別屬性。

EDIT
PHP要求$this-><class-variable>訪問內部函數類的屬性。 (請查看下面的Sectus答案。)只需使用$ pdo即可創建局部變量(在這兩種方法中),但僅在selection()中發生錯誤,因爲這是在調用query()時未首先初始化對象PDO()

+0

只是做到了這一點,它仍然說着同樣的事情: 注意:未定義變量:pdo在... 致命錯誤:調用成員函數query()中的非對象... 兩者都在同一行,這意味着$ pdo有錯誤 – GSquadron 2013-04-26 00:59:19