gpt4 book ai didi

php - 如何覆盖内置的 PHP 函数?

转载 作者:可可西里 更新时间:2023-10-31 22:12:23 26 4
gpt4 key购买 nike

我想覆盖,假设 mysql_num_rows 如下:

$dataset = array(array('id' => 1, 'name' => 'Zlatan', 'onSOF' => 1), array('id' => 1, 'name' => 'Guest', 'onSOF' => 0));

function mysql_num_rows($dataset) {
return sizeof($dataset);
}

PHP 是否支持内置函数覆盖?


扩展

我想创建一个 OpenSource 解决方案,它将覆盖所有现有的 mysql_* 函数,我将使用 PDO 实例和方法以及属性作为它们的函数体。

这意味着已经使用 mysql_* 并且发现很难完全迁移到 PDO 的用户,应该只包括这个函数覆盖,以及所有属性、函数调用、函数返回值、参数值等,应该保持不变。

最佳答案

我认为可以这样做:

//First rename existing function
rename_function('strlen', 'new_strlen');
//Override function with another
override_function('strlen', '$string', 'return override_strlen($string);');

//Create the other function
function override_strlen($string){
return new_strlen($string);
}

found it here

注意每个主机都必须有 http://php.net/manual/en/book.apd.php安装在服务器上。

编辑

另一种方法是使用命名空间

<?php
namespace mysql2pdo;
use PDO;
function mysql_connect() {
return new PDO();
}
echo mysql_connect(); // Causes error because we don't have the parameters
?>

Test it here

关于php - 如何覆盖内置的 PHP 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15230883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com