当前位置: 首页 > 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"
}


 

相关文章:

php函数 一

一 自动加载 1.1 __autoload(string $class) 类自动加载&#xff0c;7.2版本之后废弃。可使用sql_autoload_register()注册方法实现。 类自动加载&#xff0c;无返回值。 #php7.2之前function __autoload($class) {if(strpos($class, CI_) ! 0){if (file_exists(APPPATH . …...

监督学习 - 梯度提升回归(Gradient Boosting Regression)

什么是机器学习 梯度提升回归&#xff08;Gradient Boosting Regression&#xff09;是一种集成学习方法&#xff0c;用于解决回归问题。它通过迭代地训练一系列弱学习器&#xff08;通常是决策树&#xff09;来逐步提升模型的性能。梯度提升回归的基本思想是通过拟合前一轮模…...

【工具】使用ssh进行socket5代理

文章目录 shellssh命令详解正向代理&#xff1a;反向代理&#xff1a;本地 socks5 代理 shell ssh -D 3333 root192.168.0.11 #输入密码 #3333端口已经使用远程机进行转发设置Windows全局代理转发 socks127.0.0.1 3333如果远程机为公网ip&#xff0c;可通过搜索引擎查询出网…...

(delphi11最新学习资料) Object Pascal 学习笔记---第2章第六节(类型转换)

Object Pascal 学习笔记&#xff0c;Delphi 11 编程语言的完整介绍 作者: Marco Cantu 笔记&#xff1a;豆豆爸 2.6 类型转换和类型转换 ​ 正如我们所见&#xff0c;不能将一种数据类型的变量赋值给另一种类型的变量。原因在于&#xff0c;根据数据的实际表示&#xff0c;你…...

计算机服务器中了mallox勒索病毒怎么办,mallox勒索病毒解密数据恢复

企业的计算机服务器存储着企业重要的信息数据&#xff0c;为企业的生产运营提供了极大便利&#xff0c;但网络安全威胁随着技术的不断发展也在不断增加&#xff0c;近期&#xff0c;云天数据恢复中心接到许多企业的求助&#xff0c;企业的计算机服务器中了mallox勒索病毒&#…...

CPU相关专业名词介绍

CPU相关专业名词 1、CPU 中央处理器CPU&#xff08;Central Processing Unit&#xff09;是计算机的运算和控制核心&#xff0c;可以理解为PC及服务器的大脑CPU与内部存储器和输入/输出设备合称为电子计算机三大核心部件CPU的本质是一块超大规模的集成电路&#xff0c;主要功…...

VRRP协议负载分担

VRRP流量负载分担 VRRP负载分担与VRRP主备备份的基本原理和报文协商过程都是相同的。同样对于每一个VRRP备份组,都包含一个Master设备和若干Backup设备。与主备备份方式不同点在于:负载分担方式需要建立多个VRRP备份组,各备份组的Master设备可以不同;同一台VRRP设备可以加…...

maven 基本知识/1.17

maven ●maven是一个基于项目对象模型(pom)的项目管理工具&#xff0c;帮助管理人员自动化构建、测试和部署项目 ●pom是一个xml文件&#xff0c;包含项目的元数据&#xff0c;如项目的坐标&#xff08;GroupId,artifactId,version )、项目的依赖关系、构建过程 ●生命周期&…...

【Java】HttpServlet类简单方法和请求显示

1、HttpServlet类简介&#x1f340; Servlet类中常见的三个类有&#xff1a;☑️HttpServlet类&#xff0c;☑️HttpServletRequest类&#xff0c;☑️HttpResponse类 &#x1f42c;其中&#xff0c;HttpServlet首先必须读取Http请求的内容。Servlet容器负责创建HttpServlet对…...

使用Rancher管理Kubernetes集群

部署前规划 整个部署包括2个部分&#xff0c;一是管理集群部署&#xff0c;二是k8s集群部署。管理集群功能主要提供web界面方式管理k8s集群。正常情况&#xff0c;管理集群3个节点即可&#xff0c;k8s集群至少3个。本文以3节点管理集群&#xff0c;3节点k8s集群为例 说明部署过…...

QT中操作word文档

QT中操作word文档&#xff1a; 参考如下内容&#xff1a; C(Qt) 和 Word、Excel、PDF 交互总结 Qt对word文档操作总结 QT中操作word文档 Qt/Windows桌面版提供了ActiveQt框架&#xff0c;用以为Qt和ActiveX提供完美结合。ActiveQt由两个模块组成&#xff1a; QAxContainer模…...

纯前端在线Office文档安全预览之打开Word文档后禁止打印、禁止另存为、禁止复制

在一些在线Office文档中&#xff0c;有很多重要的文件需要保密控制&#xff0c;比如&#xff1a;报价单、客户资料等数据&#xff0c;只能给公司成员查看&#xff0c;但是不能编辑&#xff0c;并且不能拷贝&#xff0c;打印、不能另存为&#xff0c;导致重要资料外泄。 可以通…...

李沐深度学习-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&#xff1f; 开放式最短路径优先OSPF&#xff08;Open Shortest Path First&#xff09;是IETF组织开发的一个基于链路状态的内部网关协议&#xff08;Interior Gateway Protocol&#xff09;。 目前针对IPv4协议使用的是OSPF Version 2&#xff08;RFC2328&#x…...

Java多线程并发篇----第二十六篇

系列文章目录 文章目录 系列文章目录前言一、什么是 Executors 框架?二、什么是阻塞队列?阻塞队列的实现原理是什么?如何使用阻塞队列来实现生产者-消费者模型?三、什么是 Callable 和 Future?前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分…...

list下

文章目录 注意&#xff1a;const迭代器怎么写&#xff1f;运用场合&#xff1f; inserterase析构函数赋值和拷贝构造区别&#xff1f;拷贝构造不能写那个swap,为什么&#xff1f;拷贝构造代码 面试问题什么是迭代器失效&#xff1f;vector、list的区别&#xff1f; 完整代码 注…...

【Linux】进程间通信——system V 共享内存、消息队列、信号量

需要云服务器等云产品来学习Linux的同学可以移步/–>腾讯云<–/官网&#xff0c;轻量型云服务器低至112元/年&#xff0c;优惠多多。&#xff08;联系我有折扣哦&#xff09; 文章目录 写在前面1. 共享内存1.1 共享内存的概念1.2 共享内存的原理1.3 共享内存的使用1.3.1 …...

网络卡问题排查手段

问题 对后端来说&#xff0c;网络卡了问题&#xff0c;本身很难去排查&#xff0c;因为是 App 通过互联网连接服务 总结下&#xff0c;以往经验&#xff0c;网络卡&#xff0c;通常会有以下情况造成&#xff1a; 某地区网络问题某地区某运营商问题后端服务超载前端网络模块 …...

20240119-子数组最小值之和

题目要求 给定一个整数数组 arr&#xff0c;求 min(b) 的总和&#xff0c;其中 b 的范围涵盖 arr 的每个&#xff08;连续&#xff09;子数组。由于答案可能很大&#xff0c;因此返回答案模数 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…...

高效流感病毒A抗体:制备方法与免疫防御利器

流感病毒A&#xff08;Influenza Virus A&#xff09;&#xff0c;简称FluA&#xff0c;作为常见的呼吸道病毒&#xff0c;每年都会在全球范围内引发季节性流感爆发。它具有高度的变异性&#xff0c;能够不断进化出新的亚型&#xff0c;使得人群对其普遍易感。流感不仅会导致发…...

在AI编程时代,写技术博客还有意义吗?

在AI编程时代&#xff0c;写技术博客还有意义吗&#xff1f; 1. 引言 当GitHub Copilot、Cursor、Claude等AI编程助手能在一分钟内生成数百行代码&#xff0c;甚至能根据自然语言描述构建整个项目骨架时&#xff0c;一个尖锐的问题摆在了每一位技术人面前&#xff1a;既然AI都能…...

在龙芯3A6000/7A2000上玩转GPIO和I2C:手把手教你解读和修改固件ACPI表

龙芯平台ACPI表深度解析&#xff1a;从GPIO配置到I2C设备驱动的实战指南 当你在龙芯3A6000或7A2000开发板上连接一个温湿度传感器&#xff0c;却发现系统毫无反应时&#xff0c;问题很可能出在ACPI表的配置上。作为嵌入式开发者&#xff0c;理解并掌握ACPI表的修改技巧&#x…...

企业级应用如何借助Taotoken实现大模型API的容灾与负载均衡

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 企业级应用如何借助Taotoken实现大模型API的容灾与负载均衡 在构建依赖大模型能力的企业级应用时&#xff0c;服务的连续性与稳定性…...

用emWin定时器给你的STM32 GUI界面“注入灵魂”:实现动态数据刷新与简易动画(基于WM_TIMER消息)

用emWin定时器为STM32 GUI注入动态交互的灵魂 在嵌入式设备的人机交互设计中&#xff0c;静态界面往往给人呆板的印象。想象一下工业仪表盘上凝固的数字&#xff0c;或是医疗设备上永不变化的指示灯——这种缺乏生命力的呈现方式不仅降低用户体验&#xff0c;还可能掩盖关键数据…...

OpCore Simplify:告别繁琐配置,轻松构建黑苹果OpenCore EFI的智能工具

OpCore Simplify&#xff1a;告别繁琐配置&#xff0c;轻松构建黑苹果OpenCore EFI的智能工具 【免费下载链接】OpCore-Simplify A tool designed to simplify the creation of OpenCore EFI 项目地址: https://gitcode.com/GitHub_Trending/op/OpCore-Simplify 还在为黑…...

5分钟学会在PowerPoint中插入LaTeX公式:科研工作者的高效神器

5分钟学会在PowerPoint中插入LaTeX公式&#xff1a;科研工作者的高效神器 【免费下载链接】latex-ppt Use LaTeX in PowerPoint 项目地址: https://gitcode.com/gh_mirrors/la/latex-ppt 还在为PowerPoint里输入复杂的数学公式而头疼吗&#xff1f;作为科研人员、教师或…...

Vue3最佳实践:编写高质量代码的指南

Vue3最佳实践&#xff1a;编写高质量代码的指南 前言 各位前端小伙伴&#xff0c;不知道你们有没有遇到过这种情况&#xff1a;项目越来越大&#xff0c;代码越来越难维护&#xff01; 我曾经加入过一个Vue3项目&#xff0c;代码混乱不堪&#xff0c;维护成本极高。后来我引入了…...

X.509证书格式(SPDM协议)

字段名称含义用途示例待签名内容(tbsCertificate)Version (版本)含义: 证书版本号。取值: v1(0), v2(1), v3(2)。互联网 PKI 必须使用 v3 (值为 2)。告诉解析程序该按照哪个标准来读取后续的字段&#xff08;目前绝大多数为 v3&#xff09;。Version: 3 (0x2)Serial Number (序…...

终极指南:8步搭建你的私人游戏串流服务器Sunshine

终极指南&#xff1a;8步搭建你的私人游戏串流服务器Sunshine 【免费下载链接】Sunshine Self-hosted game stream host for Moonlight. 项目地址: https://gitcode.com/GitHub_Trending/su/Sunshine 想要在任何设备上流畅玩PC游戏吗&#xff1f;Sunshine是一款免费开源…...