0

I have few helpers - I want to redclare each helper's method as a lambda anonymous function.

I'm trying to do it by getting the helpers methods, and then doing an eval function, but it wont work, im getting parse error..

My current code:

            foreach($this->helpers as $helper)
            {
                include(master_path . 'helpers/'.$helper.'Helper.php');

                $helperClass = new applicationHelper();
                $methods = get_class_methods($helperClass);
                foreach($methods as $method )
                {

                    eval ( "\$$method = function (\$text) {
                        \$helperClass->$method(\$text);
                    }");

                }
             }

Due to efficiency fears - I'd like a better solution if you know it, thanks! Thanks Guys!

2
  • 1
    How is that question different from your Translate class function to lambda function Commented Jan 31, 2011 at 11:26
  • i'm not asking you to give me an explanation how to do it - cause i already did, but its buggy. anyway, already got my answer, thanks Commented Jan 31, 2011 at 11:31

2 Answers 2

2

That should work:

foreach($methods as $method )
{
    $$method = function($text) use ($method, $helperClass) {
        return $helperClass->$method($text);
    }
}

But still dont know why are you doing that.

EDIT PHP 5.3.x needed -> look here Anonymous funcions

Sign up to request clarification or add additional context in comments.

Comments

0
foreach ($this->helpers as $helper) {
    include(master_path . 'helpers/'.$helper.'Helper.php');

    $helperClass = new applicationHelper();
    foreach (get_class_methods($helperClass) as $method) {
        $$method = function($text) use($helperClass, $method) {
            $helperClass->$method($text);
        };
    }
}

That get's rid of the slow eval.

Comments

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.