• 杂记


    1、php子类无 构造函数,实例化子类时默认会调用父类的构造函数;子类有构造函数,实例化子类时默认不会调用父类的父类的构造函数,需要在子类中使用parent::__construct()调用父类构造函数;这些同样适用于析构函数。

    测试用例:

     1 <?php
     2 class A {
     3     public function __construct() {
     4         echo "A__construct
    ";
     5     }
     6     public function __destruct() {
     7         echo "A__destruct
    ";
     8     }
     9 }
    10 
    11 class B extends A {
    12     public function __construct() {
    13         echo "B__construct
    ";
    14     }
    15     public function __destruct() {
    16         echo "B__destruct
    ";
    17     }
    18     public function bb() {
    19         echo 'bb::';
    20         parent::__construct();
    21     }
    22 }
    23 
    24 $objB = new B();
    25 $objB->bb();
    26 ?>

    2、php函数引用返回语法:

     1 <?php
     2 class Test{
     3     private $var = array();
     4 
     5     public function &getVar() {
     6         return $this->var;
     7     }
     8 }
     9 
    10 $obj = new Test();
    11 $var = &$obj->getVar();
    12 $var['a'] = 'b';
    13 var_dump( $obj->getVar() );
    14 ?>

    3、php debug_backtrace函数使用用例:

    1 <?php
    2 // 文件 debug_backtrace1.php
    3 class D1{
    4     public function test1(){
    5         var_dump(debug_backtrace());             
    6     }
    7 }    
    8 ?>
     1 <?php
     2 // 文件 debug_backtrace2.php
     3 require "./debug_backtrace1.php";
     4 class D2{
     5     public function test1(){
     6         $d1 = new d1();
     7         $d1->test1();
     8     }
     9 }    
    10 
    11 $d2 = new D2();
    12 $d2->test1();
    13 ?>

    运行debug_backtrace2.php,结果:

     1 [sujunjie@~/work/try]$php debug_backtrace2.php 
     2 array(2) {
     3   [0] =>
     4   array(7) {
     5     'file' =>
     6     string(44) "/home/sujunjie/work/try/debug_backtrace2.php"
     7     'line' =>
     8     int(6)
     9     'function' =>
    10     string(5) "test1"
    11     'class' =>
    12     string(2) "D1"
    13     'object' =>
    14     class D1#2 (0) {
    15     }
    16     'type' =>
    17     string(2) "->"
    18     'args' =>
    19     array(0) {
    20     }
    21   }
    22   [1] =>
    23   array(7) {
    24     'file' =>
    25     string(44) "/home/sujunjie/work/try/debug_backtrace2.php"
    26     'line' =>
    27     int(11)
    28     'function' =>
    29     string(5) "test1"
    30     'class' =>
    31     string(2) "D2"
    32     'object' =>
    33     class D2#1 (0) {
    34     }
    35     'type' =>
    36     string(2) "->"
    37     'args' =>
    38     array(0) {
    39     }
    40   }
    41 }

    4、php判断常用汉字正则表达式。注意是“常用汉字”,并非“所有汉字”:

    1 <?php
    2 $p = '/^[x{4e00}-x{9fa5}]+$/u';
    3 $str = '苏俊杰';
    4 if (preg_match($p, $str, $m)) {
    5     var_dump($m);
    6 }
    7 ?>

    5、shell变量替换${}相关,摘自网络:

    用一些例子加以说明${ }的一些特异功能:
    假设我们定义了一个变量为:
    file=/dir1/dir2/dir3/my.file.txt
    我们可以用${ }分别替换获得不同的值:
    ${file#*/}:拿掉第一条/及其左边的字符串:dir1/dir2/dir3/my.file.txt
    ${file##*/}:拿掉最后一条/及其左边的字符串:my.file.txt
    ${file#*.}:拿掉第一个.  及其左边的字符串:file.txt
    ${file##*.}:拿掉最后一个.  及其左边的字符串:txt
    ${file%/*}:拿掉最后条/及其右边的字符串:/dir1/dir2/dir3
    ${file%%/*}:拿掉第一条/及其右边的字符串:(空值)
    ${file%.*}:拿掉最后一个.  及其右边的字符串:/dir1/dir2/dir3/my.file
    ${file%%.*}:拿掉第一个.  及其右边的字符串:/dir1/dir2/dir3/my


    记忆的方法为:#是去掉左边(在鉴盘上#在$之左边)
    %是去掉右边(在鉴盘上%在$之右边)
    单一符号是最小匹配﹔两个符号是最大匹配。

    ${file:0:5}:提取最左边的5个字节:/dir1
    ${file:5:5}:提取第5个字节右边的连续5个字节:/dir2

    我们也可以对变量值里的字符串作替换:
    ${file/dir/path}:将第一个dir替换为path:/path1/dir2/dir3/my.file.txt
    ${file//dir/path}:将全部dir替换为path:/path1/path2/path3/my.file.txt

  • 相关阅读:
    xml解析
    xml基础
    对象的深浅克隆
    批处理文件(bat)
    贪吃蛇逻辑代码
    排序算法
    tcp,第一个例子,客户端,服务端
    网络编程三要素
    装饰者模式
    工作中遇到的问题
  • 原文地址:https://www.cnblogs.com/sooj/p/4300932.html
Copyright © 2020-2023  润新知