2017-04-11 3 views
1

Modifier: Ceci n'est PAS une question générale sur une variable non définie mais sur cet exemple de code spécifique qui extrait une variable sans spécifier d'où. J'essaie de configurer une liste blanche de balises HTML comme document here en utilisant s9e \ TextFormatter.

Voici mon 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(); 

Cependant les variables $parser et $renderer ne sont jamais définis dans ce code d'échantillon. Je ne sais pas comment les intégrer dans ce code, n'est-ce pas?

+2

'$ parser' &' renderer' de $ pourrait être quelque chose. Peut-être revoir le code d'où vous avez copié cela pour comprendre ce que ces variables sont vraiment. – Augwa

+1

Copie possible de [PHP: "Remarque: Undefined variable", "Notice: Undefined index", et "Notice: Undefined Offset"] (http://stackoverflow.com/questions/4261133/php-notice-undefined-variable- notice-undefined-index-and-notice-undef) – Qirel

+0

ce n'est pas une question générale sur une variable indéfinie mais sur ce script spécifique. –

Répondre

1

Cette ligne

extract($configurator->finalize()); 

définit ces variables. C'est parce que extract() va "Importer des variables dans la table de symboles en cours à partir d'un tableau" 1 (en se référant à the PHP documentation example pourrait aider à comprendre cela). Regardez le docblock pour 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

Ces deux dernières options (returnParser et returnRenderer) par défaut à true.

Essayez d'exécuter ces lignes (après la configuration de l'instance Configurator):

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

Cela devrait donner ce texte:

typeof $ parser: S9e \ TextFormatter \ Parser

typeof $ rendu: s9e \ TextFormatter \ Renderers \ XSLT


http://php.net/extract

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