I have this class:
class testclass{
function func1(){
return "hello";
}
function func2(){
echo func1();
}
}
Run Code Online (Sandbox Code Playgroud)
When I am running
$test = new testclass();
$test->func2();
Run Code Online (Sandbox Code Playgroud)
I get an error: Fatal error: Call to undefined function func1() with the line index of echo func1();
My question now is, how do I make the func2 recognize func1
Is this a problem with the scopes?
如何从内部作用域访问全局变量,给定以下代码示例,如何从主函数和最内部作用域访问全局字符串 X,一旦我们退出它,最内部作用域也是可访问的主要范围还是其他范围?
#include <iostream>
#include <string>
std::string x = "global";
int counter = 1;
int main()
{
std::cout <<counter ++ << " " << x << std::endl;
std::string x = "main scope";
std::cout <<counter ++ << " " << x << std::endl;
{
std::cout <<counter ++ << " " << x << std::endl;
std::string x = "inner scope";
std::cout <<counter ++ << " " << x << std::endl;
}
std::cout <<counter++ << " " << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
目前的 …
我已经阅读了范围(@SessionScoped, @ViewScoped, @ApplicationScope and @RequestScope)类型之间的所有差异,但是,我仍然在某种程度上遇到应用程序问题。我有一个page-1 与网格和我所选择的项目发送到page-2(两者page-1并page-2用相同的支持bean),以进行编辑,然后持续存在。据我所知,我的托管bean正在使用@RequestScoped,javax.faces.bean.RequestScoped这是理想的使用范围,但是它不起作用,bean被破坏了,数据丢失了。
恢复该故事,我将注释更改为@SessionScoped并且可以正常工作,但是我想知道这是否是一个好习惯?因为我已经读到,使用这种方法不是一个好习惯,@SessionScoped因为在客户端注销之前数据将一直保持活动状态。
我有一个函数需要在一个相当紧密的循环中执行,所以它对性能很敏感。它的过滤功能旨在节省更昂贵的工作。大多数功能只是对静态列表的检查。
所以(去掉一些不相关的细节)我可以在这里做两件不同的事情:
def my_filter(arg):
SAFE_VALS = {
'a',
'b',
'f',
'i',
'm'
}
# the real list is much larger, but
# it is a static set like the above...
return arg in SAFE_VALS
Run Code Online (Sandbox Code Playgroud)
或者
# make this a module level constant
SAFE_VALS = {
'a',
'b',
'f',
'i',
'm'
}
def my_filter(arg):
return arg in SAFE_VALS
Run Code Online (Sandbox Code Playgroud)
我知道 Python 将不得不查看更高的范围级别才能找到模块级别的版本——但我不知道的是,my_filter每次运行函数时,编译版本是否有效地重新创建了这个集合,或者文字是否“烘焙” in" 到函数 def. 如果每次调用都分配一个新集合,那么我感觉使用模块级版本会更好——如果不是,我不会通过将文字提升到函数范围之外而获得任何好处。
现在分析数据足够嘈杂,我没有看到明显的区别。但是,如果该集合包含更多的长字符串,我会吗?或者,这些形式之间是否存在显着差异?