简体   繁体   English

多次包含文件时出现“无法重新声明”的问题 - PHP

[英]Problem "cannot redeclare" when the file is included more times - PHP

i'm trying to run the same file more times (again and again...) But i have this problem Cannot redeclare function我试图多次运行同一个文件(一次又一次......)但我有这个问题Cannot redeclare function

This is my code inside the file test.php这是我在文件test.php代码

$word= hello();
print_r($word);

include('test.php');    


function hello(){
    $x= "ok";
    return $x;
}

When the function print the word i want to run again the same file with include The problem is Cannot redeclare hello()当函数打印单词时,我想再次运行include的相同文件问题是Cannot redeclare hello()

DETAILS细节

To run the same file again i could to use a cronjob like to do for other files but in this project i don't know when to run again the file.要再次运行同一个文件,我可以像对其他文件一样使用cronjob ,但在这个项目中,我不知道何时再次运行该文件。 I must wait the execution of code inside test.php .我必须等待test.php中代码的执行。 The execution's time is variable and so i can't to set a cronjob when to run the file every 1 minute or 2 minute.执行的时间是可变的,因此我无法设置 cronjob 何时每 1 分钟或 2 分钟运行一次文件。

EDIT: explain more编辑:解释更多

I have a file called "A.php" where every two minutes two other files are created containing two different arrays (which we will call for simplicity array1.php and array2.php).我有一个名为“A.php”的文件,其中每两分钟创建两个其他文件,其中包含两个不同的数组(为了简单起见,我们将其称为 array1.php 和 array2.php)。

Every two minutes a cron job executes "A. php" and the code inside it destroys and creates the two new array files.每两分钟一个 cron 作业执行“A.php”,其中的代码会破坏并创建两个新的数组文件。

In my new project ("test.php") I use the arrays of the two files to send notifications to my telegram.在我的新项目(“test.php”)中,我使用这两个文件的数组向我的电报发送通知。 Notifications are sent only and exclusively if there is something new inside these 2 arrays.只有在这两个数组中有新内容时才会发送通知。

If I used a cronjob for "test.php" I could have a problem: if "A.php" for some reason failed to update the two arrays, "test.php" sends the same notifications twice.如果我将 cronjob 用于“test.php”,我可能会遇到问题:如果“A.php”由于某种原因未能更新两个数组,“test.php”会发送相同的通知两次。

For this reason I would like to run "test.php" at the end of the file: at the beginning of test.php extrapolate filemtime () of the two arrays and at the end of "test.php" extract again the filemtime () of the two arrays.出于这个原因,我想在文件末尾运行“test.php”:在 test.php 的开头外推两个数组的 filemtime () 并在“test.php”的末尾再次提取 filemtime ( ) 的两个数组。 If the two files have been updated (they have a different timestamp) I include "test.php" again, otherwise I sleep 10 seconds and repeat the comparison.如果这两个文件已更新(它们具有不同的时间戳),我将再次包含“test.php”,否则我会休眠 10 秒并重复比较。

Change your cron job so it runs test.php after A.php , but only if A.php is successful. 更改您的cron作业,以使其在A.php之后运行test.php ,但A.phpA.php成功。

*/2 * * * * php A.php && php test.php

A.php should exit with a non-zero code if it's not successful; A.php则应以非零代码退出; it can do this with 它可以做到这一点

die(1);

If you want an additional check, test.php can safe the timestamps of the files in another file. 如果要进行其他检查, test.php可以保护另一个文件中文件的时间戳。 When it starts up, it can read that file to get the previous timestamps, and compare the current timestamps with it. 启动时,它可以读取该文件以获取先前的时间戳,并与之比较当前时间戳。 If they haven't changed, it doesn't send the notification. 如果未更改,则不会发送通知。

只需将include_once用作include,就足够了


include_once "test.php";

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

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