有没有办法避免“重新声明函数”?在 PHP 中的函数内定义函数时出现致命错误?
我一直在 WordPress 的页面上实现某个插件 (dtabs),但升级到最新版本后,我发现现在第二次调用名为 dtab_list_tabs()
。
它的工作方式是,插件获得 include_once,但无论您想在布局中放置选项卡多少次,都会调用 main 函数。我对 dtab_list_tabs()
进行了 2 次此类调用。
现在的问题是,无论出于何种原因,开发人员决定直接在 dtab_list_tabs()
中包含另一个名为 current_tab()
的函数。因为它是在函数中声明的,显然 PHP 会在您第二次调用父函数时尝试重新声明它,这对我来说没有任何意义。
PHP致命错误:无法在.../wp-content/中重新声明current_tab()(之前在.../wp-content/plugins/dtabs/dtabs.php:1638中声明) plugins/dtabs/dtabs.php 第 1638 行
该修订版的代码位于 http://plugins.svn.wordpress.org/!svn/bc/208481/dtabs/trunk/dtabs.php
我想弄清楚是否有办法告诉 PHP 是的...它有一个内部函数,据我所知,这是一个完全有效的 PHP 范例,所以不要重新声明它并失败。
至于手头的情况,我已经删除了current_tab()
因为它似乎没有被使用。
I've been implementing a certain plugin (dtabs) on my page in Wordpress but after upgrading to the latest version, I found that I now have an error the 2nd time I call the main function called dtab_list_tabs()
.
The way it works is, the plugin gets include_once'd but the main function is called however many times you want to place tabs in your layout. I have 2 such calls to dtab_list_tabs()
.
Now, the problem is, for whatever reason the developer decided to include another function directly inside dtab_list_tabs()
called current_tab()
. Because it's declared within a function, apparently PHP tries to redeclare it as soon as you call the parent function the 2nd time, which doesn't make any sense to me.
PHP Fatal error: Cannot redeclare current_tab() (previously declared in .../wp-content/plugins/dtabs/dtabs.php:1638) in .../wp-content/plugins/dtabs/dtabs.php on line 1638
The code for that revision is at http://plugins.svn.wordpress.org/!svn/bc/208481/dtabs/trunk/dtabs.php
What I'm trying to figure out is whether there is a way to tell PHP that yeah... it has an internal function, which is a perfectly valid PHP paradigm as far as I know, so don't redeclare it and fail.
As for the situation at hand, I have removed current_tab()
as it doesn't appear to be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 function_exists() 来测试是否已定义具有该名称的函数。如果您将定义设为有条件的( if(something) { function foo() {...} } ),php 将仅在满足条件时“评估”定义。
另请参阅:http://docs.php.net/functions.user-define
(但我会尽量避免这样的事情)
You can use function_exists() to test if a function with that name has already been defined. If you make the definition conditional ( if(something) { function foo() {...} } ) php will "evaluate" the definition only when the condition is met.
see also: http://docs.php.net/functions.user-defined
(But I'd try to avoid such things all together)
您可以将函数声明包装在
if
语句中。使用function_exists()
查看该函数之前是否已声明。You can wrap your function declaration in an
if
statement. Usefunction_exists()
to see if the function has been previously declared or not.你可以试试这个:
You can try this: