标签: function-calls

在PHP中动态使用函数原型

我正在用PHP编写一个构造,其中解析器确定动态调用哪个函数,类似这样:

// The definition of what to call
$function_call_spec = array( "prototype"  => "myFunction", 
                             "parameters" => array( "first_par"  => "Hello",
                                                    "second_par" => "World"));

// Dispatch
$funcPrototype = $function_call_spec["prototype"];
$funcPrototype(); // Here we call function 'myFunction'.
Run Code Online (Sandbox Code Playgroud)

这一切都很好,花花公子.但现在是下一步,传递参数,我不知道是否有可能按照我想要的方式进行.它永远不会让我惊讶,但是这些天脚本语言可以做什么,所以这里是:

可以将参数传递给函数,如下所示:

// Here we call function 'myFunction' with the array of parameters.
$funcPrototype( $function_call_spec["parameters"] );
Run Code Online (Sandbox Code Playgroud)

但是,我想用明确的参数等正确地声明'myFunction':

function myFunction( $first_par, $second_par )
{

}
Run Code Online (Sandbox Code Playgroud)

接下来的问题是 - 有没有办法通过循环参数数组动态地将参数传递给函数?

为了澄清,我并不想这样做,是这样的:

$funcPrototype( $function_call_spec["parameters"]["first_par"],
                $function_call_spec["parameters"]["second_par"]  );
Run Code Online (Sandbox Code Playgroud)

因为这需要我的代码静态地知道关于myFunction的细节,这违背了整个想法.

相反,我想以某种方式这样做:

// Special magic PHP function which can be used …
Run Code Online (Sandbox Code Playgroud)

php function-calls

3
推荐指数
2
解决办法
3224
查看次数

调用函数#1时自动调用函数#2

这应该是一个快速的.有可能做这样的事情:

[Callback(funtionY)]
void functionX()
{
}

void functionY()
{
}
Run Code Online (Sandbox Code Playgroud)

那么当调用functionX时,也会自动调用functionY?我问的原因是因为我要为基于XNA的小型游戏引擎实现网络功能,我想将函数标记为Synched,以标记在调用函数时应该在所有客户端调用它.

谢谢.

c# attributes function-calls

3
推荐指数
2
解决办法
1185
查看次数

Python - 我可以访问给我打电话的对象吗?

如果我有这个:

class A:
    def callFunction(self, obj):
        obj.otherFunction()

class B:
    def callFunction(self, obj):
        obj.otherFunction()

class C:
    def otherFunction(self):
        # here I wan't to have acces to the instance of A or B who call me.

...

# in main or other object (not matter where)
a = A()
b = B()
c = C()
a.callFunction(c) # How 'c' know that is called by an instance of A...
b.callFunction(c) # ... or B
Run Code Online (Sandbox Code Playgroud)

尽管存在设计或其他问题,但这只是一个探究性思维的问题.

注意:必须在不更改 签名的情况下完成此操作otherFunction

python oop function-calls

3
推荐指数
1
解决办法
838
查看次数

我在这里遗漏了一些简单的东西(运行时执行优先级?)

执行这个简单的代码:

int  foo(int* a){
    cout <<"a="<<a;
    *a=1;
    cout <<", *a="<<*a<<endl;
    return 0;}

int main () {
    int* ptr;
    ptr=new int[2];
    ptr[0]=0;
    ptr[1]=0;

    cout<< foo(ptr) <<" "<< ptr <<" *ptr="<< *ptr <<endl;
    cout<< foo(ptr) <<" "<< ptr <<" *ptr="<< *ptr <<endl;

    return 0;}
Run Code Online (Sandbox Code Playgroud)

导致(linux):

a=0x939f008, *a=1
0 0x939f008 *ptr=0
a=0x939f008, *a=1
0 0x939f008 *ptr=1
Run Code Online (Sandbox Code Playgroud)

请解释为什么*ptr = 0在第二行,但不在第四行; 可能是,"东西" cout从右到左被"取出" ?比 - 它如何真正起作用(在运行时一步一步)?

c++ pointers side-effects function-calls operator-precedence

3
推荐指数
1
解决办法
126
查看次数

函数调用语法奇怪

我正在Python控制台上试验一些东西,我注意到函数名称和()Python 之间有多少空格并不重要,Python仍设法调用该方法?

>>> def foo():
...   print 'hello'
...
>>> foo ()
hello
>>> foo                ()
hello
Run Code Online (Sandbox Code Playgroud)

怎么可能?不应该引发某种例外吗?

python syntax function-calls python-2.7

3
推荐指数
1
解决办法
217
查看次数

R函数调用没有加载包

我想使用的功能来自Bioconductor的包hypergraph,并hyperdraw没有装载包.从小hyperdraw插图运行示例时

dh1 <- hypergraph::DirectedHyperedge("A", "B", "R1")
dh2 <- hypergraph::DirectedHyperedge(c("A", "B"), c("C", "D"), "R2")
hg <- hypergraph::Hypergraph(LETTERS[1:5], list(dh1, dh2))
hgbph <- hyperdraw::graphBPH(hg)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Error in hyperdraw::graphBPH(hg) : could not find function "hyperedges"
Run Code Online (Sandbox Code Playgroud)

如果我尝试加载hyperedges:

hyperedges <- hyperdraw:::hyperedges
Run Code Online (Sandbox Code Playgroud)

我收到了错误

Error in get(name, envir = asNamespace(pkg), inherits = FALSE) : 
  object 'hyperedges' not found
Run Code Online (Sandbox Code Playgroud)

当我使用library或加载两个包时require,我没有得到任何错误(在运行上面的代码时没有hypergraph::hyperdraw::).

为什么我不希望加载包的原因是因为我建立它采用了封装hyperdrawhypergraph只有一个功能,我宁愿把这些包装成Suggests比到Depends我的DESCRPTION文件.

有谁知道如何解决这个问题?

r function-calls package bioconductor

3
推荐指数
1
解决办法
2088
查看次数

javascript中的函数 - 定义和调用它们,有或没有函数名称

我的问题最容易解释为代码块和它下面的问题.

但这是试图用英语解释我的问题:

我正在学习JavaScript(而且我很懒,所以我使用了很多jQuery ...;))我遇到了一些我不太了解的东西,我不知道它叫什么,所以我不喜欢我知道应该研究什么术语.

基本上我想知道何时需要使用function(){ ... }(没有名称)以及何时使用function function_name(){ ... }(带有名称),以及如何使用.click() .post()或等执行函数setTimeout().

我知道一些jQuery函数,比如.click()需要你输入一个函数来调用.但问题是,你不能只说.click( function_name() )(如果我正确地解释了我的测试结果,这将在执行脚本时立即调用该函数)而不是你必须做.click( function(){ ... })或你可以做.click( function function_name(){ ... }).我还发现.click( function_name )只要事先定义就可以说,var function_name = function function_name(){ ... }而函数名是可选的,无论是否添加它都可以.

我举了一个例子,说明了我能想到的所有可能情况.我确实找到了什么工作,什么不工作,现在我想找出为什么每个人都在工作而其他人没有工作.

我希望理解这将有助于我更好地理解异步函数,如.post()setTimeout().

<button id="one">Button 1</button>
<button id="two">Button 2</button>
<button id="three">Button 3</button>
<button id="four">Button 4</button>
<button id="five">Button 5</button>
<button id="six">Button 6</button>
<button id="seven">Button 7</button>
<button id="eight">Button 8</button>
<button …
Run Code Online (Sandbox Code Playgroud)

javascript function function-calls

3
推荐指数
1
解决办法
4439
查看次数

将调用哪些订单功能

在下面的表达式中是否有一些确定的函数调用顺序,或者它是否因编译器而异?

此处的规则是否适用 - 在C中,未指定大多数运算符的函数和操作数的参数的计数顺序.在此Wiki页面中找到了上述规则

a = (f1(10, 20) * f2(30, 40)) + f3()
Run Code Online (Sandbox Code Playgroud)

c function-calls

3
推荐指数
1
解决办法
163
查看次数

这个汇编函数调用安全/完整吗?

我没有装配经验,但这是我一直在努力的.如果我缺少传递参数和通过程序集中的指针调用函数的任何基本方面,我想输入.

举例来说,如果我应该恢复我想知道ecx,edx,esi,edi.我读到它们是通用寄存器,但我找不到它们是否需要恢复?打电话后我应该做什么样的清理工作?

这是我现在的代码,它确实有效:

#include "stdio.h"

void foo(int a, int b, int c, int d)
{
  printf("values = %d and %d and %d and %d\r\n", a, b, c, d);
}

int main()
{

  int a=3,b=6,c=9,d=12;
  __asm__(
          "mov %3, %%ecx;"
          "mov %2, %%edx;"
          "mov %1, %%esi;"
          "mov %0, %%edi;"
          "call %4;"
          :
          : "g"(a), "g"(b), "g"(c), "g"(d), "a"(foo)
          );

}
Run Code Online (Sandbox Code Playgroud)

c x86 assembly function-calls inline-assembly

3
推荐指数
1
解决办法
871
查看次数

3
推荐指数
2
解决办法
102
查看次数