Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Using Anonymous Functions #3

Description

@aubreypwd

This works:

$html = new phpHtmlWriter();
echo $html->tag('hr');
echo $html->tag('pre', gffd_debug_info() );

function gffd_debug_info(){
    return "String"
}

But, this does not:

$html = new phpHtmlWriter();
echo $html->tag('hr');
echo $html->tag('pre',
    function(){
        return "String"
    }       
);

Error I get:

Catchable fatal error: Object of class Closure could not be converted to string in .../php-html-writer/lib/phpHtmlWriterElement.php on line 50

Workaround that works using call_user_func:

$html = new phpHtmlWriter();
echo $html->tag('hr');
echo $html->tag('pre',
    call_user_func(
        function(){
            return "String"
        }
    )       
);

Not really sure if you can call an anonymous function like in the second example, so the workaround may be the only option. If there's not a way, you might want to consider including this in documentation?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions