简体   繁体   English

不能重新声明函数做while循环

[英]Can not redeclare function doing while loop

I'm new to functions and I'm probably not doing it as well as I could so could use some help with this.我是函数的新手,我可能做得不够好,所以可以使用一些帮助。 I'm pulling stock information from a web sites API.我正在从网站 API 中提取股票信息。 All the information being pulled is done with several .php pages and I have one file that includes all the necessary pages.所有被提取的信息都是用几个 .php 页面完成的,我有一个包含所有必要页面的文件。 So I have something like this: pullBalanceSheet.php for all the balance sheet data pullIncomeStatement.php for all the income sheet data etc, there are 10 pages.所以我有这样的东西: pullBalanceSheet.php 用于所有资产负债表数据 pullIncomeStatement.php 用于所有收入表数据等,有 10 页。 All pages can run independently of each other and was created like this to make easy to test each API call.所有页面都可以相互独立运行,并且这样创建是为了便于测试每个 API 调用。 I have a master page (pullData.php) that contains the necessary includes for each page so it effectively runs all the pages as a single page.我有一个母版页 (pullData.php),其中包含每个页面所需的包含内容,因此它可以有效地将所有页面作为单个页面运行。 I hope this makes sense.我希望这是有道理的。

Some of my pages contain a function and this is where I'm probably not doing it the best way, but the pages work to pull the correct data.我的一些页面包含一个函数,这可能是我没有做到最好的地方,但这些页面可以提取正确的数据。 These pages pull quarterly financial information for the last 5 years.这些页面提取了过去 5 年的季度财务信息。 The issue is that every company states quarterly financial dates differently.问题是每家公司对季度财务日期的表述都不同。 So let's say it's January 2019 and some company quarterly will state Quarter 2, 2019 even though it really isn't Quarter 2 2019 until March 2019. So my code basically needs to capture possible future dates, so my code looks like this:因此,假设现在是 2019 年 1 月,一些公司的季度报告将说明 2019 年第 2 季度,即使它确实不是 2019 年第 2 季度,直到 2019 年 3 月。所以我的代码基本上需要捕获可能的未来日期,所以我的代码如下所示:

    $fiscal_year = date("Y");
    $fiscalPeriod = 'Q1';
      pulldataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey);
    $fiscalPeriod = 'Q2';
      pulldataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey);
    $fiscalPeriod = 'Q3';
      pulldataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey);
    $fiscalPeriod = 'Q4';
      pulldataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey);
    //year 2
    $fiscal_year = $fiscal_year-1;
    $fiscalPeriod = 'Q1';
      pulldataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey);
    $fiscalPeriod = 'Q2';
      pulldataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey);
    $fiscalPeriod = 'Q3';
      pulldataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey);
    $fiscalPeriod = 'Q4';
      pulldataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey);

..etc for 5 years. ..等5年。 The API looks at the $fiscal_year and the $fiscal_period and returns data if it's available. API 查看 $fiscal_year 和 $fiscal_period 并返回可用的数据。

The function is this:功能是这样的:

    function pullDataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey) {
      // function code here
    }

All my code works when pulling one stock.我所有的代码在拉一只股票时都有效。 I'm now working on pulling multiple stocks by using a while loop on my pullData.php master page and this is where I'm having the issue.我现在正在通过在我的 pullData.php 母版页上使用 while 循环来提取多只股票,这就是我遇到问题的地方。 Since the code is repeating itself for each new stock, I'm getting a fatal error cannot redeclare function.由于代码对每只新股票都在重复,我收到了一个致命错误,无法重新声明函数。 I understand why this is happening since the loop is being treated as a single page and when it hits the second stock, it errors.我理解为什么会发生这种情况,因为循环被视为单个页面,并且当它到达第二个股票时,它会出错。 I'm trying to figure out how to get around this.我想弄清楚如何解决这个问题。 I have used我用过了

    if(!function_exists('pullDataBS')) { 
      function pullDataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey) {
      //function code here
    }
    }       

And I get a undefined function pulldataBS error when it hits the function call at the very first time (3rd line of code where it tries the first pulldataBS in this example).当它第一次调用函数时,我得到一个未定义的函数 pulldataBS 错误(在这个例子中它尝试第一个 pulldataBS 的第三行代码)。 Here is my pulldata.php master page:这是我的 pulldata.php 母版页:

    while($research = mysqli_fetch_array($queryResearch)) {  
      $_SESSION['ticker']  = $research['symbol']; 
      include('pullBalanceSheet.php'); 
      include('pullIncomeStatement.php');
      // etc
    }

I'm assuming the !function_exists is not correct or I have some other critical issue with the way I coded my function.我假设 !function_exists 不正确,或者我的函数编码方式存在其他一些关键问题。 I hope this all makes sense.我希望这一切都有意义。 Let me know if you need any other code.如果您需要任何其他代码,请告诉我。 Thanks in advance!!提前致谢!!

First of all, you should not include pullBalanceSheet.php and pullIncomeStatement.php files in each iteration of while loop.首先,您不应在while循环的每次迭代中包含pullBalanceSheet.phppullIncomeStatement.php文件。 Take below two lines outside of while loop.while循环之外取下面两行。

include('pullBalanceSheet.php'); 
include('pullIncomeStatement.php');

Now comes to the problem, you must not declare your function(s) over and over again, just declare it once and call it (with appropriate arguments) multiple times as per your need.现在问题来了,你不能一遍又一遍地声明你的函数,只需声明一次并根据需要多次调用它(使用适当的参数)。 Having said that, you can use defined() along with define() functions to solve your function redelaration problem.话虽如此,您可以使用defined()define()函数来解决您的函数重新定义问题。

if(!defined('PULL_DATA_BS')){
    define('PULL_DATA_BS', 'pullDataBS');

    function pullDataBS($fiscal_year,$fiscalPeriod, $username,$password,$apiKey) {
        //function code here
    }
}

This way this function won't get declared more than once.这样这个函数就不会被多次声明。 Ideally, you are supposed to encapsulate the entire code of a file with below block.理想情况下,您应该使用以下块封装文件的整个代码。

xyz.php xyz.php

<?php
    if(!defined('XYZ_PHP')){
        define('XYZ_PHP', 'xyz.php');

        // file code

    }
?>

This way, you will prevent yourself(and somebody else) from including this file code more than once.这样,您将防止自己(和其他人)多次包含此文件代码。

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

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