1

Edit: This is NOT a general question about undefined variable but about this specific code example that pulls variable without specifying from where.

I'm trying to setup an HTML whitelist of tags as documented here using s9e\TextFormatter.

Here is my code:

use s9e\TextFormatter\Configurator;

function htmlFormat( )
{
    $configurator = new Configurator;
    $configurator->plugins->load( 'HTMLElements' );

    $configurator->HTMLElements->allowElement( 'b' );
    $configurator->HTMLElements->allowAttribute( 'b', 'class' );
    $configurator->HTMLElements->allowElement( 'i' );

    // Get an instance of the parser and the renderer
    extract( $configurator->finalize() );

    $text = '<b>Bold</b> and <i>italic</i> are allowed, but only <b class="important">bold</b> can use the "class" attribute, not <i class="important">italic</i>.';
    $xml = $parser->parse( $text );
    $html = $renderer->render( $xml );
}

htmlFormat();

However the variables $parser and $renderer are never defined in that sample code. I don't know how to integrate them in this code, do you?

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Robert Brax
  • 6,508
  • 12
  • 40
  • 69
  • 2
    `$parser` & `$renderer` could be anything. Perhaps review the code where you copied this from to understand what those variables really are. – Jonathan Apr 11 '17 at 15:44
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Qirel Apr 11 '17 at 15:46
  • it's not a general question about undefined variable but about this specific script. – Robert Brax Apr 11 '17 at 15:47
  • Looking at [this example](https://github.com/s9e/TextFormatter/blob/master/docs/examples/01_verbose.php) `$parser` is apparently defined by the call to [extract()](http://php.net/extract) on line 59 – Sᴀᴍ Onᴇᴌᴀ Apr 11 '17 at 15:53
  • 1
    Are you seeing an error in your IDE/output or just noticing that they are undefined? – Sᴀᴍ Onᴇᴌᴀ Apr 11 '17 at 16:01

1 Answers1

1

This line

extract( $configurator->finalize() );

defines those variables. This is because extract() will "Import variables into the current symbol table from an array"1 (referring to the PHP documentation example might help with understanding this). Look at the docblock for Configurator::finalize():

/**
* Finalize this configuration and return all the relevant objects
*
* Options: (also see addHTMLRules() options)
*
*  - addHTML5Rules:    whether to call addHTML5Rules()
*  - finalizeParser:   callback executed after the parser is created (gets the parser as arg)
*  - finalizeRenderer: same with the renderer
*  - optimizeConfig:   whether to optimize the parser's config using references
*  - returnParser:     whether to return an instance of Parser in the "parser" key
*  - returnRenderer:   whether to return an instance of Renderer in the "renderer" key
*
* @param  array $options
* @return array One "parser" element and one "renderer" element unless specified otherwise
*/

2

Those last two options (returnParser and returnRenderer) default to true.

Try running these lines (after configuring the Configurator instance):

extract( $configurator->finalize() );
echo 'typeof $parser: '.get_class($parser).'<br>';
echo 'typeof $renderer: '.get_class($renderer).'<br>';

This should yield this text:

typeof $parser: s9e\TextFormatter\Parser

typeof $renderer: s9e\TextFormatter\Renderers\XSLT


1http://php.net/extract

2https://github.com/s9e/TextFormatter/blob/master/src/Configurator.php#L223

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58