php面向对象精要(3)
PHP 面向对象 精要
2023-09-11 14:19:51 时间
1,final关键字定义的方法,不能被重写
由于final修饰了show方法,子类中重写show方法会报错
<?php class MyClass { final function show(){ echo "hello world" . PHP_EOL; } } class MyTest extends MyClass { function show(){ echo __CLASS__ . PHP_EOL; } } ?>
2,final定义的class不能被继承
<?php final class MyClass { function show(){ echo "hello world" . PHP_EOL; } } class MyTest extends MyClass { } ?>
3,__toString方法
如果定义了__toString方法,打印一个对象时,将调用__toString
class Person { private $name; function __construct( $name ){ $this->name = $name; } function __toString(){ return $this->name; } } $p = new Person( "ghostwu" ); var_dump( $p ); echo PHP_EOL; print $p . PHP_EOL;
4, 异常处理( try, catch, throw )
>异常处理类都应该继承自系统自带的Exception
>异常抛出时( throw 异常对象 ),会一个个查找catch后面的异常处理,如果匹配到,就执行,后面的catch就算能匹配,也不会执行
Exception类摘要:
Exception { /* 属性 */ protected string $message ; protected int $code ; protected string $file ; protected int $line ; /* 方法 */ public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] ) final public string getMessage ( void ) final public Throwable getPrevious ( void ) final public int getCode ( void ) final public string getFile ( void ) final public int getLine ( void ) final public array getTrace ( void ) final public string getTraceAsString ( void ) public string __toString ( void ) final private void __clone ( void ) }
1 class NullHandleException extends Exception { 2 function __construct( $message ){ 3 parent::__construct( $message ); 4 } 5 } 6 7 function printObj( $obj ){ 8 if( $obj == NULL ) { 9 throw new NullHandleException( "printObj接收到一个null对象" ); 10 } 11 print $obj . PHP_EOL; 12 } 13 14 class Person { 15 private $name; 16 function __construct( $name ) { 17 $this->name = $name; 18 } 19 function __toString() { 20 return $this->name; 21 } 22 } 23 24 try { 25 printObj( new Person( "ghostwu" ) ); 26 printObj( null ); 27 printObj( new Person( "zhangsan" ) ); 28 }catch( NullHandleException $exception ){ 29 print $exception->getMessage() . PHP_EOL; 30 print "in file:" . $exception->getFile() . PHP_EOL; 31 print "in line:" . $exception->getLine() . PHP_EOL; 32 }catch( Exception $exception ){ 33 echo "这个异常分支不会被执行" . PHP_EOL; 34 print $exception->getMessage() . PHP_EOL; 35 } 36 37 echo "try...catch处理完毕" . PHP_EOL;
输出结果:
ghostwu@ubuntu:~/php_study/php5/03$ php -f exception_usage.php ghostwu printObj接收到一个null对象 in file:/home/ghostwu/php_study/php5/03/exception_usage.php in line:10 try...catch处理完毕
没有输出"zhangsan", 因为printObj( null );抛出了异常,因此"zhangsan"被忽略
5,__autoload自动加载文件
在项目开发中,经常需要在一个文件中包含多个通用的库文件,这个时候会产生一大堆的require或者include,而使用__autoload函数可以简化函数的加载过程
1,MyClass.php
1 class MyClass { 2 function printHelloWorld(){ 3 echo "Hello World" . PHP_EOL; 4 } 5 }
2,common.inc
function __autoload( $className ) { require_once( "./{$className}.php" ); }
3,main.php
require_once "common.inc"; $obj = new MyClass(); $obj->printHelloWorld();
相关文章
- PHP面向对象07_PDO
- PHP如何自动识别第三方Restful API的内容,自动渲染成 json、xml、html、serialize、csv、php等数据
- PHP垃圾回收机制防止内存溢出
- 【PHP面向对象(OOP)编程入门教程】23.自动加载类 __autoload()函数
- 【PHP面向对象(OOP)编程入门教程】19.抽象方法和抽象类(abstract)
- PHP程序员,因该养成 7 个面向对象的好习惯
- php:使用XHProf查找PHP性能瓶颈
- php分享二十九:命名空间
- PHP(面向对象)连接数据库,实现基本的增删改查
- file not found. nginx php nginx 如何开启解析 PHP 的功能
- php5.6源码安装及php-fpm配置与nginx集成
- php webservice
- php:用数组实现多语言(PHP 7.4.2)
- PHP命名空间规则解析及高级功能3
- [PHP] Xhprof 非侵入式使用指南
- [php-src]Php扩展的内存泄漏处理思路
- [转]php 解决json_encode中文UNICODE转码问题
- Atitit php pdo的api使用 目录 1.1. PHP PDO简介1 1.2. 若要使用数据库长连接,:PDO::ATTR_PERSISTENT1 2. 其他设置2 2.1. )、P
- atitit。全局变量的设计与实现 java php的异同
- paip. 解决php 以及 python 连接access无效的参数量。参数不足,期待是 1”的错误
- PHP 5 Date/Time 函数
- 封装自己的DB类(PHP)