0

我正在尝试使用 try catch 块中的属性。try catch 块在类内部。我想扩展一个特定的类,使其成为处理异常的类的子类。问题是,当我尝试使用子类中的这些变量时,它总是说未定义。我必须删除这两个类才能捕获属性。通过在try catch块内部添加return语句(我添加了return 1)在此处阅读了其他一些答案后,它似乎不起作用,并且总是说未定义的变量。有什么帮助吗?

语言是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';
?>
4

2 回答 2

0
  1. 你没有子类。对象继承
  2. 你没有属性。特性

阅读类和对象


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();
            }
        }
    }
于 2013-04-26T00:33:31.280 回答
0

放在$pdo前面connection()

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

在函数内部,它是一个局部变量,而不是类级别的属性。

编辑
PHP 需要$this-><class-variable>访问函数内部的类属性。(在下面查看@Sectus 的答案。)仅使用 $pdo 正在动态创建一个局部变量(在这两种方法中),但只会给出错误,selection()因为这是在没有首先初始化对象的query()情况下被调用的地方。PDO()

于 2013-04-26T00:51:20.310 回答