gpt4 book ai didi

php - 命名空间环境中的 PHP 类的快捷方式

转载 作者:行者123 更新时间:2023-12-04 16:53:59 26 4
gpt4 key购买 nike

这是我挑剔的...但目前我必须使用 \在我所有的类(class)面前...

    //credit card expiration years and months
$expirationYears = array();
$expirationMonths = array();
$datePeriod = new \DatePeriod(new \DateTime(), new \DateInterval("P1Y"), 5);
foreach ($datePeriod as $year) {
$expirationYears[] = $year->format('Y');
}
$datePeriod = new \DatePeriod(new \DateTime('January 1st'), new \DateInterval("P1M"), 11);
foreach ($datePeriod as $month) {
$class = new \stdClass();
$class->value = $month->format('m');
$class->label = $month->format('M');
$expirationMonths[] = $class;
}

有没有办法解决这个问题,或者只是命名空间环境中需要的东西?

我认为指定 use 是不合理的对于每个类(class)……这太乏味了,无法维护

最佳答案

use关键字是正确的方法。这不是太多的开销。

我们在一个项目中大量使用它,这没什么大不了的。
现在一直使用反斜杠很乏味。

此外,跟踪您在某些可能长达数千行的文件中使用的所有类也很棒。因此,您可以转到顶部并查看正在使用的类(不使用文本搜索)。

关于php - 命名空间环境中的 PHP 类的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13290762/

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