2013-04-26 46 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