功能定义顺序重要吗? [英] Does function definition order matter?

查看:84
本文介绍了功能定义顺序重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的脚本中,是否声明了项目的顺序是重要的?

例如,如果add_action指向尚未定义的函数,

  add_action('load-categories.php')是否有关系或函数声明是否总是在其所调用的代码之前? ,'my_admin_init'); 
函数my_admin_init(){
//做某事
}


解决方案

如果函数是在调用之前或之后声明的,但函数应该在脚本中并且应该被加载,这并不重要。



这是第一种方法,它可以工作:

  some_func($一,$ b); 

函数some_func($ a,$ b)
{
echo'Called';
}

这是第二种方法,也可以工作:

function some_func($ a,$ b)
{
echo'Called';
}

some_func($ a,$ b);


In the script below, does the order in which items are declared matter?

For example, if the add_action points to a function that has not yet been defined? Does it matter or should the function declaration always precede any code in which its called?

add_action('load-categories.php', 'my_admin_init');
function my_admin_init(){
//do something
}

解决方案

That doesn't matter if the function is declared before or after the call but the function should be there in the script and should be loaded in.

This is the first method and it will work:

some_func($a,$b);

function some_func($a,$b)
{
    echo 'Called';
}

This is the second method and will also work:

function some_func($a,$b)
{
    echo 'Called';
}

some_func($a,$b);

这篇关于功能定义顺序重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆