gpt4 book ai didi

php - 覆盖类内的预定义函数。

转载 作者:行者123 更新时间:2023-12-04 17:03:46 25 4
gpt4 key购买 nike

我想使用命名空间覆盖类中的预定义函数

// one.class.php

class One{
public function __construct($str){
echo strtolower($str);
}
}

好的,调用这会导致
new One("mIxEd"); // mixed

我想覆盖 strtolower 里面的功能 一级

我试图这样做
// blabla.class.php
namespace blabla;
function strtolower($str){
return strtoupper($str);
}
class One extends \One{};

好的,当我把它们放在一起时
// script.php
require_once("one.class.php");
require_once("blabla.class.php");
new One("mIxEd"); // mixed
new blabla\One("mIxEd"); // mixed

输出仍然是小写的,所以我不会在第一类中覆盖 strtolower。

如果可能的话,我正在徘徊......
谢谢你的帮助!

最佳答案

您需要覆盖 blabla\One 中的构造函数以调用命名空间的 strtolower。

class One extends \One {
public function __construct($str) {echo strtolower($str);}
}

关于php - 覆盖类内的预定义函数。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23564905/

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