classFoo{// static methodpublicstaticfunctionsbar(){echo"foosbar!";}// normal methodpublicfunctionbar(){echo"foobar!";}}// normal functionfunctionbar(){echo"bar!";}$name="bar";$sname="sbar";$name();$foo=newFoo();$foo->{$name}();$foo::$sname();
#!/usr/bin/python#coding: UTF-8"""@author: CaiKnife根据函数名称动态调用"""# normal functiondefdo_foo():print"foo!"classPrint():# normal methoddefdo_foo(self):print"foo!"# static method@staticmethoddefstatic_foo():print"static foo!"defmain():obj=Print()func_name="do_foo"static_name="static_foo"# use eval() to get the string value from a variableeval(func_name)()# use getattr() to get method from a class or an instance# sometimes you have to use is_callable() to make sure that it is a method but not an attrgetattr(obj,func_name)()getattr(Print,static_name)()if__name__=='__main__':main()