zoukankan      html  css  js  c++  java
  • PHP Exception

    PHP Exception

    官方文档:http://cn2.php.net/exceptions

    Deom 1

    <?php
    
    class Conf {
        private $file;
        private $xml;
        private $lastmatch;
    
        function __construct( $file ) {
            $this->file = $file;
            if ( ! file_exists( $file ) ) {
                throw new Exception( "file '$file' does not exist" );
            }
            $this->xml = simplexml_load_file($file);
        }    
    
        function write() {
            if ( ! is_writeable( $this->file ) ) {
                throw new Exception("file '{$this->file}' is not writeable");
            }
            file_put_contents( $this->file, $this->xml->asXML() );
        }
    
        function get( $str ) {
            $matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");
            if ( count( $matches ) ) {
                $this->lastmatch = $matches[0];
                return (string)$matches[0];
            }
            return null;
        }
    
        function set( $key, $value ) {
            if ( ! is_null( $this->get( $key ) ) ) {
                $this->lastmatch[0]=$value;
                return;
            }
            $conf = $this->xml->conf;
            $this->xml->addChild('item', $value)->addAttribute( 'name', $key );
        }
    }
    
    
    try {
        //$conf = new Conf( dirname(__FILE__)."/conf01.xml" );
        //$conf = new Conf( dirname(__FILE__)."/conf.unwriteable.xml" );
        $conf = new Conf( "nonexistent/not_there.xml" );
        print "user: ".$conf->get('user')."\n";
        print "host: ".$conf->get('host')."\n";
        $conf->set("pass", "newpass");
        $conf->write();
    } catch ( Exception $e ) {
        die( $e->__toString() );
    }
    
    ?>

    Deom 2

    <?php
    
    class XmlException extends Exception {
        private $error;
    
        function __construct( LibXmlError $error ) {
            $shortfile = basename( $error->file );
            print_r( $error );
            $msg = "[{$shortfile}, line {$error->line}, col {$error->column}] {$error->message}";
            $this->error = $error;
            parent::__construct( $msg, $error->code );
        }
    
        function getLibXmlError() {
            return $this->error;
        }
    }
    
    class FileException extends Exception { }
    class ConfException extends Exception { }
    
    class Conf {
        private $file;
        private $xml;
        private $lastmatch;
    
        function __construct( $file ) {
            $this->file = $file;
            if ( ! file_exists( $file ) ) {
                throw new FileException( "file '$file' does not exist" );
            }
            $this->xml = simplexml_load_file($file, null, LIBXML_NOERROR );
            if ( ! is_object( $this->xml ) ) {
                throw new XmlException( libxml_get_last_error() );
            }
            $matches = $this->xml->xpath("/conf");
            if ( ! count( $matches ) ) {
                throw new ConfException( "could not find root element: conf" );
            }
        }    
    
        function write() {
            if ( ! is_writeable( $this->file ) ) {
                throw new Exception("file '{$this->file}' is not writeable");
            }
            file_put_contents( $this->file, $this->xml->asXML() );
        }
    
        function get( $str ) {
            $matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");
            if ( count( $matches ) ) {
                $this->lastmatch = $matches[0];
                return (string)$matches[0];
            }
            return null;
        }
    
        function set( $key, $value ) {
            if ( ! is_null( $this->get( $key ) ) ) {
                $this->lastmatch[0]=$value;
                return;
            }
            $conf = $this->xml->conf;
            $this->xml->addChild('item', $value)->addAttribute( 'name', $key );
        }
    }
    
    
    class Runner {
        static function init() { 
            try {
                $conf = new Conf( dirname(__FILE__)."/conf.broken.xml" );
                print "user: ".$conf->get('user')."\n";
                print "host: ".$conf->get('host')."\n";
                $conf->set("pass", "newpass");
                $conf->write();
            } catch ( FileException $e ) {
                // permissions issue or non-existent file
                throw $e;
            } catch ( XmlException $e ) {
                // broken xml
            } catch ( ConfException $e ) {
                // wrong kind of XML file
            } catch ( Exception $e ) {
                // backstop: should not be called
            }
        }
    }
    
    Runner::init();
    
    ?>
  • 相关阅读:
    Eclipse的SVN插件与本地SVN客户端关联不上
    Oracle与MySQL连接配置
    树莓派实践项目
    20135333苏正生——信息安全系统设计基础第十四周学习总结
    《图解tcp/ip》读书笔记(二)
    解决VC几个编译问题的方法——好用
    《深入理解计算机系统》深入实践之——vim深入研究
    20135333苏正生——信息安全系统设计基础第十二周学习总结
    《图解tcp/ip》读书笔记(一)
    《你的灯亮着吗?:发现问题的真正所在》读书笔记2
  • 原文地址:https://www.cnblogs.com/Athrun/p/php__exception.html
Copyright © 2011-2022 走看看