note 92851 added to function.eval

From: Date: Tue, 11 Aug 2009 22:27:08 +0000
Subject: note 92851 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-159346@lists.php.net to get a copy of this message
eval and namespace For those who wonder: since eval executes the code in another context than the current script, it is possible to evaluate code that uses a particular namespace without changing the current one (and it does not trigger an error if there is no namespace and the evaluated one is not at the top of the script). Exemple: <?php namespace Foo; echo 'namespace 1: '.__NAMESPACE__."\n"; eval('namespace Bar; class BarClass {} echo \'namespace 2: \'.__NAMESPACE__."\n";'); echo 'namespace 1 again: '.__NAMESPACE__."\n"; ?> output: namespace 1: Foo namespace 2: Bar namespace 1 again: Foo And it will create the class Bar\BarClass. Also, the eval code will not belong to the namespace of the code that do the eval: <?php namespace Foo; echo 'namespace 1: '.__NAMESPACE__."\n"; eval('class BarClass {} echo \'namespace 2: \'.__NAMESPACE__."\n";'); ?> output: namespace 1: Foo namespace 2: // global namespace ---- Server IP: 195.221.21.36 Probable Submitter: 82.225.179.89 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- https://master.php.net/note/edit/92851 Del: integrated -- https://master.php.net/note/delete/92851/integrated Del: useless -- https://master.php.net/note/delete/92851/useless Del: bad code -- https://master.php.net/note/delete/92851/bad+code Del: spam -- https://master.php.net/note/delete/92851/spam Del: non-english -- https://master.php.net/note/delete/92851/non-english Del: in docs -- https://master.php.net/note/delete/92851/in+docs Del: other reasons-- https://master.php.net/note/delete/92851 Reject -- https://master.php.net/note/reject/92851 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159346) next »