错误:不在对象上下文中时使用'$ this'

时间:2013-11-22 23:08:51

标签: php arrays recursion this

我一直在编码和改进代码 jszobody 为我提供了可以在此LINK上找到的代码,它让我成为了我的参考但却导致了错误{{1} }

PHP:

Fatal error: Using $this when not in object context

预期输出:

  

数组([0] => HappyOutgoingTallHandsome 1 => HappyOutgoingTallPlain [2] => HappyOutgoingTallUgly [3] => HappyOutgoingShortHandsome [4] => HappyOutgoingShortPlain [5] => HappyOutgoingShortUgly [ 6] => HappyOutgoingMediumHandsome [7] => HappyOutgoingMediumPlain [8] => HappyOutgoingMediumUgly [9] => HappyIntrovertedTallHandsome [10] => HappyIntrovertedTallPlain [11] => HappyIntrovertedTallUgly [12] => HappyIntrovertedShortHandsome [13 ] => HappyIntrovertedShortPlain [14] => HappyIntrovertedShortUgly [15] => HappyIntrovertedMediumHandsome [16] => HappyIntrovertedMediumPlain [17] => HappyIntrovertedMediumUgly [18] => SadOutoveTallHandsome [19] => SadOutgoingTallPlain [20] => SadOutgoingTallUgly [21] => SadOutgoingShortHandsome [22] => SadOutgoingShortPlain [23] => SadOutgoingShortUgly [24] => SadOutgoingMediumHandsome [25] => SadOutgoingMediumPlain [26] => SadOutgoingMediumUgly [27] = > SadIntroverted TallHandsome [28] => SadIntrovertedTallPlain [29] => SadIntrovertedTallUgly [30] => SadIntrovertedShortHandsome [31] => SadIntrovertedShortPlain [32] => SadIntrovertedShortUgly [33] => SadIntrovertedMediumHandsome [34] => SadIntrovertedMediumPlain [35] => SadIntrovertedMediumUgly [36] => AngryOutgoingTallHandsome [37] => AngryOutgoingTallPlain [38] => AngryOutgoingTallUgly [39] => AngryOutgoingShortHandsome [40] => AngryOutgoingShortPlain [41] => AngryOutgoingShortUgly [42] => AngryOutgoingMediumHandsome [43] => AngryOutgoingMediumPlain [44] => AngryOutgoingMediumUgly [45] => AngryIntrovertedTallHandsome [46] => AngryIntrovertedTallPlain [47] => AngryIntrovertedTallUgly [48] => AngryIntrovertedShortHandsome [49] => AngryIntrovertedShortPlain [50] => AngryIntrovertedShortUgly [51] => AngryIntrovertedMediumHandsome [52] => AngryIntrovertedMediumPlain [53] => AngryIntrovertedMediumUgly [54] => HopefulOutgoingTallHandsome [55] => HopefulOutgoingTallPlain [56] => HopefulOutgoingTallUgly [57] => HopefulOutgoingShortHandsome [58] => HopefulOutgoingShortPlain [59] => HopefulOutgoingShortUgly [60] => HopefulOutgoingMediumHandsome [61] => HopefulOutgoingMediumPlain [62] => HopefulOutgoingMediumUgly [63] => HopefulIntrovertedTallHandsome [64] => HopefulIntrovertedTallPlain [65] => HopefulIntrovertedTallUgly [66] => HopefulIntrovertedShortHandsome [67] => HopefulIntrovertedShortPlain [68] => HopefulIntrovertedShortUgly [69] => HopefulIntrovertedMediumHandsome [70] => HopefulIntrovertedMediumPlain [71] => HopefulIntrovertedMediumUgly [72] => )

我哪里出错?

2 个答案:

答案 0 :(得分:2)

你有很多对类变量的引用,比如:

$this->permutationsResultsArray = array(); 

PHP抱怨因为这个函数不是类中的方法。如果您只是删除this->,那么它将会有效:

$permutationsResultsArray = array(); 

此外,当你完成所有工作后,你永远不会真正返回结果..就像这样:

return $permutationsResultsArray;

虽然有问题。你正在重复使用,除了在第一轮中你没有创建那个数组,但是你的代码使用它就像定义它一样。

BTW:使用3个foreach循环可以更轻松地完成您的功能:

function getCombinations($traits)
{
    $combinations = array('');
    foreach( $traits as $trait_level ) {
        $new_combinations = array();
        foreach ( $combinations as $comb ) {
            foreach ( $trait_level as $trait ){
                $new_combinations[] = "$comb $trait";
            }
        }
        $combinations = $new_combinations;
    }
    return $combinations;
}

答案 1 :(得分:0)

函数没有自引用$this。实际上,删除所有$this->引用并使$ tempArray按引用而不是按值传递,您的代码应该可以工作...

function getAllPossiblePermutations($mdArray,  $firstTime=true,  &$tempArray=array())