1

I'm trying to retrieve permutation set based on a string. However, i couldn't execute the function properly. I'm not really good with public static, or private or how should I call the function.

<?php namespace Helpers;

class Helper {

public static function permute($str,$i,$n) 
{
   if ($i == $n)
       return "$str\n";
   else {
        for ($j = $i; $j < $n; $j++) {
          swap($str,$i,$j);
          permute($str, $i+1, $n);
          swap($str,$i,$j); // backtrack.
       }
   }
}

public static function swap(&$str,$i,$j) {
    $temp = $str[$i];
    $str[$i] = $str[$j];
    $str[$j] = $temp;
}}

This is how I call the function from my controller.

Helper::permute($str,0,strlen($str))

I'm getting this error:

Call to undefined function Helpers\swap()

1 Answer 1

3

You're calling the method swap() and permute() from a static context, but handle them as if they were non-static.
Try changing it to the following:

public static function permute($str,$i,$n) 
{
    if ($i == $n)
        return "$str\n";
    else {
        for ($j = $i; $j < $n; $j++) {
            self::swap($str,$i,$j);
            self::permute($str, $i+1, $n);
            self::swap($str,$i,$j); // backtrack.
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Changed the method of call for 3 lines into self:: but wonder why it's not returning any value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.