当前位置: 首页 > news >正文

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)

五 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"
}


 

http://www.lryc.cn/news/284843.html

相关文章:

  • 监督学习 - 梯度提升回归(Gradient Boosting Regression)
  • 【工具】使用ssh进行socket5代理
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第六节(类型转换)
  • 计算机服务器中了mallox勒索病毒怎么办,mallox勒索病毒解密数据恢复
  • CPU相关专业名词介绍
  • VRRP协议负载分担
  • maven 基本知识/1.17
  • 【Java】HttpServlet类简单方法和请求显示
  • 使用Rancher管理Kubernetes集群
  • QT中操作word文档
  • 纯前端在线Office文档安全预览之打开Word文档后禁止打印、禁止另存为、禁止复制
  • 李沐深度学习-d2lzh_pytorch模块实现
  • 什么是OSPF?为什么需要OSPF?OSPF基础概念
  • Java多线程并发篇----第二十六篇
  • list下
  • 【Linux】进程间通信——system V 共享内存、消息队列、信号量
  • 网络卡问题排查手段
  • 20240119-子数组最小值之和
  • c# 释放所有嵌入资源, 到某个本地文件夹
  • Unity SnapScrollRect 滚动 匹配 列表 整页
  • 网络命令ping和telnet
  • ros2学习笔记-CLI工具,记录命令对应操作。
  • 自然语言处理的发展
  • flink operator 拉取阿里云私有镜像(其他私有类似)
  • C语言算法赛——蓝桥杯(省赛试题)
  • 【文本到上下文 #2】:NLP 的数据预处理步骤
  • Minio文件分片上传实现
  • C语言总结十一:自定义类型:结构体、枚举、联合(共用体)
  • 解决Spring Boot应用打包后文件访问问题
  • 循环神经网络的变体模型-LSTM、GRU