note 114107 deleted from language.namespaces.rationale by salathe

From: Date: Thu, 13 Feb 2014 16:59:35 +0000
Subject: note 114107 deleted from language.namespaces.rationale by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198501@lists.php.net to get a copy of this message
Note Submitter: Nehuen ---- Los namespace no afectan a las variables superglobales, por lo que $_POST es el mismo en todos los namespaces al igual que el resto de las s-g. Si se quiere un namespace realmente aislado, donde el script no tenga ningun conocimiento del contexto en que fue llamado se puede usar algo como esto: <?php // File aislado.php namespace local_ws; class simulateContext { private $sc_GLOBALS = array(); private $sc_SERVER = array(); private $sc_GET = array(); private $sc_POST = array(); private $sc_FILES = array(); private $sc_COOKIE = array(); private $sc_SESSION = array(); private $sc_REQUEST = array(); private $sc_ENV = array(); public function load() { $this->sc_GLOBALS = $GLOBALS; $this->sc_SERVER = $_SERVER; $this->sc_GET = $_GET; $this->sc_POST = $_POST; $this->sc_FILES = $_FILES; $this->sc_COOKIE = $_COOKIE; $this->sc_SESSION = $_SESSION; $this->sc_REQUEST = $_REQUEST; $this->sc_ENV = $_ENV; } public function save() { $GLOBALS = $this->sc_GLOBALS; $_SERVER = $this->sc_SERVER; $_GET = $this->sc_GET; $_POST = $this->sc_POST; $_FILES = $this->sc_FILES; $_COOKIE = $this->sc_COOKIE; $_SESSION = $this->sc_SESSION; $_REQUEST = $this->sc_REQUEST; $_ENV = $this->sc_ENV; } public function __SET($seccion, $array) { $this->{'sc_'.$seccion} = $array; } public function __GET($seccion) { return $this->{'sc_'.$seccion}; } public function clearGlobal() { $GLOBALS = array(); $_SERVER = array(); $_GET = array(); $_POST = array(); $_FILES = array(); $_COOKIE = array(); $_SESSION = array(); $_REQUEST = array(); $_ENV = array(); } public function clearThis() { $this->sc_GLOBALS = array(); $this->sc_SERVER = array(); $this->sc_GET = array(); $this->sc_POST = array(); $this->sc_FILES = array(); $this->sc_COOKIE = array(); $this->sc_SESSION = array(); $this->sc_REQUEST = array(); $this->sc_ENV = array(); } } function showLocalSGloblal() { // Crear una copia del contexto superglobal $contextoGlobal = new simulateContext(); $contextoGlobal->load(); $contextoGlobal->clearGlobal(); // Crear un nuevo contexto para la funcion $contextoWs = new simulateContext(); $contextoWs->POST = array('foo'=>123,'bar'=>'test'); $contextoWs->save(); // ... // // Aqui tu codigo aislado // ... // // Recuperar las superglobales originales $contextoGlobal->save(); $contextoGlobal->clearThis(); } ?> <?php include('aislado.php'); $_POST['foo'] = 'test'; $_POST['bar'] = 'example'; echo('En el contexto principal estan definidas estas variables: <br>'); foreach($_POST as $indice => $valor) { echo('<br><b>'.$indice.': -> </b>'.$valor); } echo('<hr>En el namespace estan definidas estas variables: <br>'); local_ws\showLocalSGloblal(); echo('<hr>En el contexto principal estan definidas estas variables: <br>'); foreach($_POST as $indice => $valor) { echo('<br><b>'.$indice.': -> </b>'.$valor); }

« previous php.notes (#198501) next »