-1

When I call any function without recursion (class is loaded by SPL) - all fine, but if that function is calling itself (recursion) - nothing works.

If I use function without autoloader - all works great. I think that happens because object of class doesn't exist, like with magic methods: you have to use __callStatic, not a __call with abstract using class, I was trying to make this function static oO but nothing works again.

Any ideas how does it possible to use recursion through autoloader?

For example this function from php.net doesn't work in autoloader mode:

function r_implode($glue, $pieces)
{
    foreach ($pieces as $r_pieces)
    {
        if (is_array( $r_pieces )) 
        {
            $r_pieces = r_implode($glue, $r_pieces); 
        }
        else 
        {
            $retVal[] = $r_pieces; 
        }
    }
    return implode($glue, $retVal);
}

class load
{
    public static function init()
    {
        return spl_autoload_register(array(__CLASS__, "hook"));
    }
    public static function quit()
    {
        return spl_autoload_unregister(array(__CLASS__, "hook"));
    }
    public static function hook($class)
    {
        // echo "CLASS IS:$class<br>";
        $lnk=PATH . str_replace("_", "/", $class) . ".php";
        ob_start();
        require $lnk;
        ob_end_clean();
        return $class;
    }
}

So when I add function into a class tools, and call tools::r_implode($a,$b); function doesn't work, but when I insert this function in the same php and call r_implode($a,$b) works.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 1
    CODE!! Post your code or we can't help you – mowwwalker Jan 25 '12 at 02:33
  • There are many functions on php.net. In fact, all of them. Post the code YOU are using that isn't working – mowwwalker Jan 25 '12 at 02:36
  • What do you mean autoloader? POST YOUR CODE. What are you running, what are you expecting, and what's happening? – mowwwalker Jan 25 '12 at 02:42
  • Code works: http://codepad.viper-7.com/ZaxkAU – mowwwalker Jan 25 '12 at 02:54
  • sorry. but i know that code works, it doesnt works in a abstract class class::function(); – user1112984 Jan 25 '12 at 02:55
  • great, iv got -1, and no one answer main question: does it possible to use recursion function in a abstract classes... OFCOUSE CODE WORK, else i wouldnt post it, omg why all of you think, that everyone without high rating is a dumbass – user1112984 Jan 25 '12 at 03:03
  • @user1112984: It's not clear from your question what your problem is and where it is located. Please bear in mind that many users here are willing to understand you, but you should show some efforts if you would like to get precise answers. For example, the code was not properly formatted which made it hard to read. Additionally it's not clear how your code is related to your question. E.g. if you post code that works but your question is about some other code that does not work, it's not really helpful. You should post the code that does not work instead. – hakre Jan 25 '12 at 11:04

1 Answers1

0

From your posted info this is not clear. You didn't describe the actual error. But I surmise your problem is actually this:

class tools {

    function r_implode($glue, $pieces)
    {
        $r_pieces = r_implode($glue, $r_pieces); 
    }
}

You have packed that function into a class, and the autoloader may even find it. But you didn't adapt the recursive call. If you don't use tools::r_implode for the recursion, then PHP won't find that function. Static methods need to be named explicitly (with class:: prefix). Keeping the plain function name there won't work.

mario
  • 144,265
  • 20
  • 237
  • 291