简体   繁体   English

Drupal PHP执行两次

[英]Drupal PHP executing twice

After running a drush update on my drupal 7 site, the php code that I use to bring in user information for a form executes twice. 在我的drupal 7网站上运行drush更新后,我用来为表单引入用户信息的php代码执行两次。 This is a problem because I have created some functions in the php that get called so when it executes the PHP the second time it tries to re-declare the functions and I get errors like this: 这是一个问题,因为我已经在php中创建了一些被调用的函数,因此当它第二次执行PHP时,它将尝试重新声明这些函数,并且出现如下错误:

PHP Fatal error:  Cannot redeclare fooBar() (previously declared in [path_to_drupal7]/modules/php/php.module(80) : eval()'d code:3) in [path_to_drupal7]/modules/php/php.module(80) : eval()'d code on line 4

It doesn't matter what the function is called or what it does. 调用什么函数或做什么都没有关系。 In this example case this is the code: 在此示例中,代码如下:

<?php
function fooBar() {
    print "foo bar";
}

fooBar();
?>

It also doesn't seem to matter what content type the page is (I my case I need it to bring user information into a form). 页面的内容类型似乎也无关紧要(我需要将用户信息带入表单中)。

Why is Drupal executing the PHP twice? 为什么Drupal执行两次PHP? And more importantly, how can I keep it from doing so? 更重要的是,我如何才能避免这样做?

EDIT: Drupal seems to be executing the php once for the trimmed version and once for full version and once for the full version. 编辑:Drupal似乎针对修剪版本执行一次php,针对完整版本执行一次,针对完整版本执行一次。 This is what I expect when I preview the post. 这是我预览帖子时的期望。 I don't really care about the preview version so I would be happy to get rid of it. 我不太在意预览版本,因此很高兴摆脱它。 Why is Drupal executing the code twice when I view the page? 为什么查看页面时Drupal两次执行代码? (why is it running it for the trimmed version when I'm actually viewing the page?) (为什么当我实际查看页面时,为什么要针对修剪后的版本运行它?)

Something must be re-including/requiring the file containing this function or you have this function within a loop. 必须重新包含/要求包含此功能的文件,否则您将在循环中使用此功能。 Find that, or, wrap it with function_exists . 找到它,或者用function_exists包装它。

<?php

if (!function_exists('fooBar')) {
    function fooBar() {
        print "foo bar";
    }
}

fooBar();

Like you said, the php you entered as input is being eval'd twice....once for the teaser, and once for the full body text. 就像您说的那样,您作为输入输入的php被评估两次……一次是针对预告片,一次是针对全文。

Best practice : Don't define functions with eval. 最佳实践 :不要使用eval定义函数。 It can get messy fast. 它会很快变得混乱。

Better practice : eval = evil. 更好的做法 :eval =邪恶。 Don't use it. 不要使用它。

In the time it takes to debug the problem, you could have written a very basic module that defines fooBar, then you can execute it from your PHP input field if you want. 在调试问题时,您可能已经编写了定义fooBar的非常基本的模块,然后可以根据需要在PHP输入字段中执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM