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 >= 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 >= 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"
}
相关文章:
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 . …...
监督学习 - 梯度提升回归(Gradient Boosting Regression)
什么是机器学习 梯度提升回归(Gradient Boosting Regression)是一种集成学习方法,用于解决回归问题。它通过迭代地训练一系列弱学习器(通常是决策树)来逐步提升模型的性能。梯度提升回归的基本思想是通过拟合前一轮模…...
【工具】使用ssh进行socket5代理
文章目录 shellssh命令详解正向代理:反向代理:本地 socks5 代理 shell ssh -D 3333 root192.168.0.11 #输入密码 #3333端口已经使用远程机进行转发设置Windows全局代理转发 socks127.0.0.1 3333如果远程机为公网ip,可通过搜索引擎查询出网…...
(delphi11最新学习资料) Object Pascal 学习笔记---第2章第六节(类型转换)
Object Pascal 学习笔记,Delphi 11 编程语言的完整介绍 作者: Marco Cantu 笔记:豆豆爸 2.6 类型转换和类型转换 正如我们所见,不能将一种数据类型的变量赋值给另一种类型的变量。原因在于,根据数据的实际表示,你…...
计算机服务器中了mallox勒索病毒怎么办,mallox勒索病毒解密数据恢复
企业的计算机服务器存储着企业重要的信息数据,为企业的生产运营提供了极大便利,但网络安全威胁随着技术的不断发展也在不断增加,近期,云天数据恢复中心接到许多企业的求助,企业的计算机服务器中了mallox勒索病毒&#…...
CPU相关专业名词介绍
CPU相关专业名词 1、CPU 中央处理器CPU(Central Processing Unit)是计算机的运算和控制核心,可以理解为PC及服务器的大脑CPU与内部存储器和输入/输出设备合称为电子计算机三大核心部件CPU的本质是一块超大规模的集成电路,主要功…...
VRRP协议负载分担
VRRP流量负载分担 VRRP负载分担与VRRP主备备份的基本原理和报文协商过程都是相同的。同样对于每一个VRRP备份组,都包含一个Master设备和若干Backup设备。与主备备份方式不同点在于:负载分担方式需要建立多个VRRP备份组,各备份组的Master设备可以不同;同一台VRRP设备可以加…...
maven 基本知识/1.17
maven ●maven是一个基于项目对象模型(pom)的项目管理工具,帮助管理人员自动化构建、测试和部署项目 ●pom是一个xml文件,包含项目的元数据,如项目的坐标(GroupId,artifactId,version )、项目的依赖关系、构建过程 ●生命周期&…...
【Java】HttpServlet类简单方法和请求显示
1、HttpServlet类简介🍀 Servlet类中常见的三个类有:☑️HttpServlet类,☑️HttpServletRequest类,☑️HttpResponse类 🐬其中,HttpServlet首先必须读取Http请求的内容。Servlet容器负责创建HttpServlet对…...
使用Rancher管理Kubernetes集群
部署前规划 整个部署包括2个部分,一是管理集群部署,二是k8s集群部署。管理集群功能主要提供web界面方式管理k8s集群。正常情况,管理集群3个节点即可,k8s集群至少3个。本文以3节点管理集群,3节点k8s集群为例 说明部署过…...
QT中操作word文档
QT中操作word文档: 参考如下内容: C(Qt) 和 Word、Excel、PDF 交互总结 Qt对word文档操作总结 QT中操作word文档 Qt/Windows桌面版提供了ActiveQt框架,用以为Qt和ActiveX提供完美结合。ActiveQt由两个模块组成: QAxContainer模…...
纯前端在线Office文档安全预览之打开Word文档后禁止打印、禁止另存为、禁止复制
在一些在线Office文档中,有很多重要的文件需要保密控制,比如:报价单、客户资料等数据,只能给公司成员查看,但是不能编辑,并且不能拷贝,打印、不能另存为,导致重要资料外泄。 可以通…...
李沐深度学习-d2lzh_pytorch模块实现
d2lzh_pytorch 模块 import random import torch import matplotlib_inline from matplotlib import pyplot as plt import torchvision import torchvision.transforms as transforms import torchvision.datasets import sys from collections import OrderedDict# --------…...
什么是OSPF?为什么需要OSPF?OSPF基础概念
什么是OSPF? 开放式最短路径优先OSPF(Open Shortest Path First)是IETF组织开发的一个基于链路状态的内部网关协议(Interior Gateway Protocol)。 目前针对IPv4协议使用的是OSPF Version 2(RFC2328&#x…...
Java多线程并发篇----第二十六篇
系列文章目录 文章目录 系列文章目录前言一、什么是 Executors 框架?二、什么是阻塞队列?阻塞队列的实现原理是什么?如何使用阻塞队列来实现生产者-消费者模型?三、什么是 Callable 和 Future?前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分…...
list下
文章目录 注意:const迭代器怎么写?运用场合? inserterase析构函数赋值和拷贝构造区别?拷贝构造不能写那个swap,为什么?拷贝构造代码 面试问题什么是迭代器失效?vector、list的区别? 完整代码 注…...
【Linux】进程间通信——system V 共享内存、消息队列、信号量
需要云服务器等云产品来学习Linux的同学可以移步/–>腾讯云<–/官网,轻量型云服务器低至112元/年,优惠多多。(联系我有折扣哦) 文章目录 写在前面1. 共享内存1.1 共享内存的概念1.2 共享内存的原理1.3 共享内存的使用1.3.1 …...
网络卡问题排查手段
问题 对后端来说,网络卡了问题,本身很难去排查,因为是 App 通过互联网连接服务 总结下,以往经验,网络卡,通常会有以下情况造成: 某地区网络问题某地区某运营商问题后端服务超载前端网络模块 …...
20240119-子数组最小值之和
题目要求 给定一个整数数组 arr,求 min(b) 的总和,其中 b 的范围涵盖 arr 的每个(连续)子数组。由于答案可能很大,因此返回答案模数 Example 1: Input: arr [3,1,2,4] Output: 17 Explanation: Subarrays are [3]…...
c# 释放所有嵌入资源, 到某个本地文件夹
版本号 .net 8 代码 using System.Reflection;namespace Demo;internal class Program {static void Main(string[] args){// 获取当前 执行exe 的目录 / 当前命令行所在的目录 var currentDir Directory.GetCurrentDirectory();Console.WriteLine(currentDir);Extract…...
10-红外接收探头电路设计实战指南
1. 红外接收探头基础入门 第一次接触红外接收探头时,我也被那一堆专业术语搞得晕头转向。其实这东西就像个"红外线翻译官",专门把遥控器发来的红外光信号转换成电信号。市面上常见的HS0038、LF0038L这些型号,本质上都是将光敏二极…...
GPU资源管理混乱?nvitop一站式解决方案深度解析
GPU资源管理混乱?nvitop一站式解决方案深度解析 【免费下载链接】nvitop An interactive NVIDIA-GPU process viewer and beyond, the one-stop solution for GPU process management. 项目地址: https://gitcode.com/gh_mirrors/nv/nvitop 在深度学习训练、…...
告别手动标注!用RexUniNLU零样本模型自动提取电商评论情感
告别手动标注!用RexUniNLU零样本模型自动提取电商评论情感 1. 电商评论分析的痛点与解决方案 电商平台每天产生海量用户评论,这些评论蕴含着宝贵的用户反馈和市场洞察。传统的情感分析方法通常面临两大难题: 标注成本高:需要大…...
EVA-01保姆级教程:Qwen2.5-VL-7B多模态大模型在EVA-01中的本地化安全部署
EVA-01保姆级教程:Qwen2.5-VL-7B多模态大模型在EVA-01中的本地化安全部署 1. 引言:欢迎来到NERV指挥中心 想象一下,你面前有一个能看懂图片、理解图表、甚至能和你讨论图片里发生了什么的智能助手。现在,我们把这个助手装进了一…...
如何为SortableJS实现高效自动化测试:拖拽功能的完整测试指南
如何为SortableJS实现高效自动化测试:拖拽功能的完整测试指南 【免费下载链接】Sortable Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required. 项目地址: https://gitcode.com/gh_mirrors/so/Sortable …...
终极指南:用Java打造你的专属微信机器人 - 深入解析wechat-api框架
终极指南:用Java打造你的专属微信机器人 - 深入解析wechat-api框架 【免费下载链接】wechat-api 🗯 wechat-api by java7. 项目地址: https://gitcode.com/gh_mirrors/we/wechat-api 想象一下这样的场景:每天早上7点,你的微…...
保研党必看:用本科论文逆袭IEEE二区期刊的5个关键操作(含时间管理秘籍)
保研党必看:用本科论文逆袭IEEE二区期刊的5个关键操作(含时间管理秘籍) 在保研竞争日益激烈的当下,一篇高质量的学术论文往往能成为决定成败的关键。对于大多数本科生来说,科研经历有限、资源匮乏是普遍面临的困境。但…...
从SuperGlue到LoFTR:无检测器特征匹配是如何“卷”出来的?技术演进深度解读
从SuperGlue到LoFTR:无检测器特征匹配的技术革命与范式迁移 在计算机视觉领域,特征匹配一直是三维重建、SLAM、图像配准等任务的核心基础。传统方法如SIFT、ORB等基于手工设计的特征检测与描述算法,在过去二十年里主导了这一领域。然而&#…...
LangChain4j vs Spring AI:Java AI 框架技术选型深度对比与生产落地指南
LangChain4j vs Spring AI:Java AI 框架技术选型深度对比与生产落地指南 摘要:当 Java 团队建设 AI 应用时,真正困难的通常不是“能否调通模型”,而是“如何把 Prompt、RAG、工具调用、可观测性、限流熔断、灰度发布、权限隔离与业务系统稳定地耦合起来”。本文不再停留在 …...
从C语言到裸机运行:i.MX6ULL 的 GPIO 控制与编译链接过程分析
引言在嵌入式系统开发中,从高级语言到硬件控制的完整链路涉及编译、链接、寄存器配置等多个环节。本文基于 i.MX6ULL 平台,以 C 语言实现 LED 与蜂鸣器控制为例,系统分析 ARM 裸机开发中的编译工具链使用、链接脚本的作用,以及 GP…...
