php函数 一

一 自动加载

1.1 __autoload(string $class)

类自动加载,7.2版本之后废弃。可使用sql_autoload_register()注册方法实现。

类自动加载,无返回值。

#php7.2之前

function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        if (file_exists(APPPATH . 'core/'. $class . EXT)) {
            @include_once( APPPATH . 'core/'. $class . EXT );
        }elseif(file_exists(LIBS_PATH . 'core/'. $class . EXT)) {
            @include_once( LIBS_PATH . 'core/'. $class . EXT );
        }
    }
}

1.2 spl_autoload_functions()

获取已注册的函数。返回数组。

1.3 spl_autoload_unregister(callable $callback)

注销已注册的函数。返回布尔值

1.4 spl_autoload_call(string $class)

请求已注册类。无返回值。执行对应类的构造。

#测试文件 test1.php php 7.2 之后

defined('APPPATH') or define('APPPATH', "./autoload");
defined('DS') or define('DS', DIRECTORY_SEPARATOR);

class Load {
    public static function autoload() {
        $dir = APPPATH;
        $paths = [];
        if (is_dir($dir)) {
            $handle = opendir($dir);
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    $info = pathinfo($file);
                    if ("php" == $info['extension']) {
                        $path = $dir . DS . $file;
                        @include_once $path;
                        $paths[] = $path;
                    }
                }
            }
            closedir($handle);
        }
        return $paths;
    }
}
spl_autoload_register(["Load", 'autoload'], true, true);
// $test1 = spl_autoload_call('Test1', true, true);
// $test1->run();
$list = spl_autoload_functions();
var_dump($list);
$test1 = new Test1();
$test1->run();
spl_autoload_call("Test1");
spl_autoload_unregister(["Load", 'autoload']);
$list = spl_autoload_functions();
var_dump($list);

 文件夹结构:autoload/test1.php  autoload/test2.php。

#test1.php
class Test1 {
    public function __construct() {
        echo "构造:" . __CLASS__ . PHP_EOL;
    }
    public function run() {
        var_dump(__CLASS__);
    }
}

#test2.php
class Test2 {
    public function run() {
        var_dump(__CLASS__);
    }
}

 测试结果

array(1) {
  [0] =>
  array(2) {
    [0] =>
    string(4) "Load"
    [1] =>
    string(8) "autoload"
  }
}
构造:Test1
string(5) "Test1"
array(0) {
}

1.5 spl_autoload_extensions(?string $file_extensions = null)

加载并返回的默认扩展。返回字符串。

1.6 spl_autoload(string $class, ?string $file_extensions = null)

__autoload()函数的默认实现。需要传入类名,找不到对应类会报错。最好对应类设置命名空间,防止类名重复导致加载错误

测试1

set_include_path(APPPATH);
spl_autoload_extensions('.php');
spl_autoload_register();
$list = spl_autoload_functions();
var_dump($list);
$test1 = new Test1();
$test1->run();

测试结果

array(1) {
  [0] =>
  string(12) "spl_autoload"
}
构造:Test1
string(5) "Test1"

测试2

#test1.php
namespace app;
class Test1 {
    public function __construct() {
        echo "构造:" . __CLASS__ . PHP_EOL;
    }
    public function run() {
        var_dump(__CLASS__);
    }
}

#test2.php
namespace app;
class Test2 {
    public function run() {
        var_dump(__CLASS__);
    }
}


#./autoload/config.json
{
    "app\\Test1":"test1.php",
    "app\\Test2":"test2.php"
}
function autoload_test3() {
    $config_file = APPPATH . DS . "config.json";
    if (!is_file($config_file)) {
        return false;
    }
    $config = json_decode(file_get_contents($config_file), true);
    foreach ($config as $key => $value) {
        $file = APPPATH . DS . $value;
        if (is_file($file)) {
            @include_once $file;
        }
        $classname = $key;
        spl_autoload($classname);
    }
}



spl_autoload_register('autoload_test3');
$list = spl_autoload_functions();
var_dump($list);
$test1 = new app\Test2();
$test1->run();

 测试结果

array(1) {
  [0] =>
  string(14) "autoload_test3"
}
string(9) "app\Test2"

二 debug_backtrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0)

limit:用于限制返回堆栈帧的数量。默认为(limit=0),返回所有的堆栈帧。

options:

1)DEBUG_BACKTRACE_PROVIDE_OBJECT、1:object、args两个索引都包括

2)0:仅包括args索引

3)DEBUG_BACKTRACE_IGNORE_ARGS、2:两个索引都不包括

4)DEBUG_BACKTRACE_PROVIDE_OBJECT|DEBUG_BACKTRACE_IGNORE_ARGS、3:仅包括object索引

#test2.php
namespace app;
class Test2 {
    public function run() {
        var_dump(__CLASS__);
    }
    public function test($args) {
        var_dump(debug_backtrace());
    }
}

 测试

spl_autoload_register('autoload_test3');
$list = spl_autoload_functions();
$test1 = new app\Test2();
$test1->test(['qwe' => 123]);

测试结果

array(2) {
  [0] =>
  array(7) {
    'file' =>
    string(32) "D:\workspace\php\test\test11.php"
    'line' =>
    int(68)
    'function' =>
    string(4) "test"
    'class' =>
    string(9) "app\Test2"
    'object' =>
    class app\Test2#1 (0) {
    }
    'type' =>
    string(2) "->"
    'args' =>
    array(1) {
      [0] =>
      array(1) {
        'qwe' =>
        int(123)
      }
    }
  }
  [1] =>
  array(4) {
    'file' =>
    string(32) "D:\workspace\php\test\test11.php"
    'line' =>
    int(70)
    'function' =>
    string(5) "test3"
    'args' =>
    array(0) {
    }
  }
}

preg_replace_callback_array

参数1 pattern:以正则表达式为key的数组,value为匿名函数。

参数2 $subject: 需处理的字符串。字符串或数组。

参数3 $count: 替换次数

参数4 $flags:影响匹配数组的格式。值为PREG_OFFSET_CAPTURE或PREG_UNMATCHED_AS_NULL。

PREG_OFFSET_CAPTURE:匹配返回时会附加字符串偏移量。返回配置数据中包含字符串开始的位置。

PREG_UNMATCHED_AS_NULL:使用该标记,未匹配的子组会报告为 null;未使用时,报告为空的 string。

#测试
$str = '<p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5, PHP 7, PHP 8)</p>';
$pattern = [
        '/(?<=class=").*(?=">)/' => function ($match) {
            var_dump($match);
            return '123';
        },
        '/php+/i' => function ($match) {
            return "~php~";
        },
];
$result = preg_replace_callback_array($pattern, $str);
var_dump($result);

#测试结果
string(67) "<p class="123">(~php~ 4 &gt;= 4.0.1, ~php~ 5, ~php~ 7, ~php~ 8)</p>"

四 迭代

4.1 is_iterable(mixed $value)

判断是否可迭代。返回布尔值。

4.2 iterator_count(Traversable|array $iterator))

对迭代器中的元素计数。不能保留指针位置。例如以下例子,不设置$iterator->rewind()则$iterator->valid()返回false。

使用$iterator->count()不会影响指针。

4.3 iterator_to_array(Traversable|array $iterator, bool $preserve_keys = true)

preserve_keys:是否使用迭代器元素键作为索引

$arr = ["test1", "asd", "qw"];
if (is_iterable($arr)) {
        $iterator = new ArrayIterator($arr);
        var_dump($iterator->count());
        var_dump(iterator_count($iterator));
        $iterator->rewind();
        while ($iterator->valid()) {
            $count = $iterator->count();
            echo "count:" . $count . PHP_EOL;
            $item = $iterator->current();
            $key = $iterator->key();
            $count = $iterator->count();
            echo "key:" . $key . " item:" . $item . " count:" . $count . PHP_EOL;
            $iterator->next();
        }
        $arr2 = iterator_to_array($iterator, true);
        var_dump($arr2);
        var_dump(iterator_count($iterator));
}

 测试结果

int(3)
int(3)
count:3
key:0 item:test1 count:3
count:3
key:1 item:asd count:3
count:3
key:2 item:qw count:3
array(3) {
  [0] =>
  string(5) "test1"
  [1] =>
  string(3) "asd"
  [2] =>
  string(2) "qw"
}
int(3)

4.4 reset(array|object &$array)

将数组的内部指针指向第一个单元

function test7() {
    $arr = ['qwe', 'asd', 'zxc'];
    next($arr);
    var_dump(current($arr));
    reset($arr);
    var_dump(current($arr));
}
test7();

五 func_get_args()

获取函数参数列表的数组。返回数组

function test6() {
    $numargs = func_num_args();
    $arg_list = func_get_args();
    $param = [];
    if (1 == $numargs) {
        $first = $arg_list[0];
        if (is_string($first)) {
            $param = $arg_list;
        }
        return $param;
    } else {
        $obj = new ArrayObject($arg_list);
        $iterator = $obj->getIterator();
        $use_list = [];
        while ($iterator->valid()) {
            $item = $iterator->current();
            if (is_numeric($item)) {
                $item = (string) $item;
            }
            if (is_string($item)) {
                $item = test6($item);
            }
            if (is_array($item)) {
                $use_list = array_merge($use_list, $item);
            }
            $iterator->next();
        }
        // return $use_list;
        var_dump($use_list);
    }
}

test6(1, 2, 3);

 测试结果

array(3) {
  [0] =>
  string(1) "1"
  [1] =>
  string(1) "2"
  [2] =>
  string(1) "3"
}


 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lsswear

感谢大佬打赏 q(≧▽≦q)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值