gpt4 book ai didi

php - 如何在php中的类中包含变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:51:19 25 4
gpt4 key购买 nike

我有一些文件test.php

<?PHP
$config_key_security = "test";
?>

我有一些课

test5.php

 include test.php
class test1 {
function test2 {
echo $config_key_security;
}
}

最佳答案

   class test1 {
function test2 {
global $config_key_security;
echo $config_key_security;
}
}

   class test1 {
function test2 {
echo $GLOBALS['config_key_security'];
}
}

让您的类依赖全局变量并不是真正的最佳实践 - 您应该考虑将其传递给构造函数。

关于php - 如何在php中的类中包含变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/797380/

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