简体   繁体   English

PHP __DIR__ 效率

[英]PHP __DIR__ efficiency

I have a question about the magic variables.我有一个关于魔法变量的问题。 Are these true variables or are they a type of function call?这些是真正的变量还是 function 调用的一种类型? Being as how PHP is an interpreted language I am very picky about how I assign variables.作为 PHP 是一种解释语言,我对如何分配变量非常挑剔。 I do not like to call a function multiple times if I can store the response in a variable and reuse it without costing the CPU time over and over.我不喜欢多次调用 function 如果我可以将响应存储在一个变量中并重复使用它而不会一遍又一遍地花费 CPU 时间。 Currently, I assign DIR to a local variable and use that variable for all of my includes and anything that requires a current directory path.目前,我将DIR分配给一个局部变量,并将该变量用于我的所有包含以及需要当前目录路径的任何内容。 I am wondering if this is saving CPU time for running a function.我想知道这是否可以节省运行 function 的 CPU 时间。 I have spent some time looking around the internet but I can't find anything that says how these magic variables are assigned.我花了一些时间在互联网上寻找,但我找不到任何说明这些魔法变量是如何分配的。

example例子

$dir = __DIR__;

require_once $dir . 'Some Class to Include';
require_once $dir . 'Another Class';

All these "magical" constants are resolved at compile time, unlike regular constants, which are resolved at runtime.所有这些“神奇”的常量都在编译时解析,不像常规常量在运行时解析。

https://www.php.net/manual/en/language.constants.magic.php https://www.php.net/manual/en/language.constants.magic.php

So no, you are not saving any CPU time所以不,你没有节省任何 CPU 时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM