简体   繁体   English

无法重新声明先前声明的PHP函数

[英]Cannot redeclare function previously declared php

I'm running into an "Cannot redeclare" error and I can't figure out how to fix it. 我遇到了“无法重新声明”错误,我不知道如何解决它。 So I have a few functions in a php file located below. 所以我在下面的PHP文件中有一些功能。 Now these functions iterate over an array of data. 现在,这些函数遍历数据数组。

I think I've surmised that the problem is that I'm looping the function over and over again in the foreach loop, and its the foreach loop thats been the problem. 我想我已经推测出问题是我在foreach循环中一遍又一遍地循环了函数,而它的foreach循环就是问题所在。 It seems like its already writing one the function to memory the first time and then for some reason it doesn't like being evoked again. 似乎它已经第一次将一个函数写入内存,然后由于某种原因它不希望再次被唤醒。

Your help appreciated. 感谢您的帮助。

PS I've seen a number of similar posts on the issue such as Fatal error: Cannot redeclare but that doesn't seem to work. PS我在此问题上看到过许多类似的帖子,例如致命错误:无法重新声明,但这似乎不起作用。

<?php
// *****Code Omitted from Stack**** 


function postHelper($data, $field1, $field2)
{ //TODO Abstract and make sure post Helper  and modify Post can be the same thing.
    $result = array();
    for ($j = 0; $j < count($data); ++$j) { //iterator over array
        if ($field2 == "") {
            $result[$j] = $data[$j][$field1];
        } else {
            return $result[$j] = $data[$j][$field1][$field2];
        }
    }
    return $result;


}

//returns an array with only @ and # values 

function modifyPost($data)
{

//puts symbol @ before read data

    function addSymbol($data, $field1, $field2)
    {
        $info = postHelper($data, $field1, $field2);
        foreach ($info as &$n) {
            $n = '@' . $n;
        }
        print_r($info);
    }


    /*
     Parse texts and returns an array with only @ or # signs used
    */


    function parseText($data)
    {

        $newarr = array();
        $text = postHelper($data, "text", "");

        foreach ($text as &$s) { //separates into words
            $ex = explode(" ", $s);
            foreach ($ex as &$n) { //if text doesnt' begin with '@' or '#' then throw it out.
                if (substr($n, 0, 1) === '@' || strpos($n, '#') !== false) {
                    array_push($newarr, $n . ',');
                }

            }

        }

        return $newarr;
    }
}


foreach ($posts as $entry) {
    if (!function_exists('modifyPost')) {
        $nval = "hello";
        modifyPost($entry);
        $entry['mod_post'] = $nval;
    }
}

?>

EDIT: I've solved the error. 编辑:我已经解决了错误。 Turns out that the original posts did actually work. 原来的帖子确实有效。 I messed in naming. 我搞砸了命名。 I will give points to anyone who can explain to me why this is necessary for a call. 我会给任何可以向我解释为什么打电话的必要条件的人。 Moreover, I will update post if there is an additional questions that I have. 此外,如果还有其他问题,我将更新帖子。

The error says it all. 错误说明了一切。 You have duplicate modifyData() & parseText functions. 您有重复的modifyData()parseText函数。

Remove the top half of the php file so only one of each occurs. 删除php文件的上半部分,以便每个仅出现一个。

Php doesn't support nested functions. PHP不支持嵌套函数。 Although you technically can declare a function within a function: 尽管从技术上讲,您可以在函数内声明一个函数:

function modifyPost($data)
{


    function addSymbol($data, $field1, $field2)

the inner function becomes global, and the second attempt to declare it (by calling the outer function once again) will fail. 内部函数变为全局,并且第二次尝试声明它(通过再次调用外部函数)将失败。

This behaviour seems counter-intuitive, but this is how it works at the moment. 这种行为似乎违反直觉,但这是目前的工作方式。 There's RFC about real nested functions, which also lists several workarounds for the problem. 有关于真正的嵌套函数的RFC ,它也列出了解决该问题的几种方法。

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

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