静态关键字里面的函数? [英] `static` keyword inside function?
本文介绍了静态关键字里面的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关键字 static
对函数中的变量做了什么?
function module_load_all($ bootstrap = FALSE){
static $ has_run = FALSE
解决方案
它使函数记住给定变量的值( $ has_run
in您可以将这个用于不同的目的,例如:
> 函数doStuff(){
static $ cache = null;
if($ cache === null){
$ cache ='%heavy stuff stuff or something%';
}
//使用$ cache的代码
}
在这个例子中, if if
只会执行一次。即使多次调用 doStuff
也会发生。
I was looking at the source for Drupal 7, and I found some things I hadn't seen before. I did some initial looking in the php manual, but it didn't explain these examples.
What does the keyword static
do to a variable inside a function?
function module_load_all($bootstrap = FALSE) {
static $has_run = FALSE
解决方案
It makes the function remember the value of the given variable ($has_run
in your example) between multiple calls.
You could use this for different purposes, for example:
function doStuff() {
static $cache = null;
if ($cache === null) {
$cache = '%heavy database stuff or something%';
}
// code using $cache
}
In this example, the if
would only be executed once. Even if multiple calls to doStuff
would occure.
这篇关于静态关键字里面的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文