Phar::setStub

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 1.0.0)

Phar::setStubUtilizado para especificar el cargador PHP o el contenedor de carga de un archivo Phar

Descripción

public Phar::setStub(resource|string $stub, int $length = -1): bool

Nota:

Este método requiere que la variable de configuración INI phar.readonly esté definida a 0 para funcionar con los objetos Phar. De lo contrario, se lanzará una excepción PharException.

Este método se utiliza para añadir un cargador PHP a un nuevo archivo Phar, o para reemplazar el contenedor de carga de un archivo Phar existente.

El contenedor de carga de un archivo Phar se utiliza cuando un archivo es incluido directamente como en este ejemplo:

<?php
include 'monphar.phar';
?>

El cargador no se utiliza cuando un fichero es incluido a través del flujo phar como esto:

<?php
include 'phar://monphar.phar/unfchier.php';
?>

Parámetros

stub

Una cadena o un resource de flujo abierto a utilizar como contenedor ejecutable para este archivo phar.

length

Valores devueltos

Esta función retorna true en caso de éxito o false si ocurre un error.

Errores/Excepciones

Una excepción UnexpectedValueException es lanzada si phar.readonly está activado en el php.ini. Una excepción PharException es lanzada si se encuentran problemas al escribir los cambios en el disco.

Historial de cambios

Versión Descripción
8.3.0 Llamar a Phar::setStub() con una resource y una length ahora está obsoleto. Tales llamadas deberían ser reemplazadas por: $phar->setStub(stream_get_contents($resource));

Ejemplos

Ejemplo #1 Un ejemplo con Phar::setStub()

<?php
try {
$p = new Phar(dirname(__FILE__) . '/nouveau.phar', 0, 'nouveau.phar');
$p['a.php'] = '<?php var_dump("Salut");';
$p->setStub('<?php var_dump("Premier"); Phar::mapPhar("nouveau.phar"); __HALT_COMPILER(); ?>');
include
'phar://nouveau.phar/a.php';
var_dump($p->getStub());
$p['b.php'] = '<?php var_dump("Tout le monde");';
$p->setStub('<?php var_dump("Second"); Phar::mapPhar("nouveau.phar"); __HALT_COMPILER(); ?>');
include
'phar://nouveau.phar/b.php';
var_dump($p->getStub());
} catch (
Exception $e) {
echo
'Error al escribir nuevo.phar: ', $e;
}
?>

El ejemplo anterior mostrará :

string(5) "Salut"
string(79) "<?php var_dump("Premier"); Phar::mapPhar("nouveau.phar"); __HALT_COMPILER(); ?>"
string(13) "Tout le monde"
string(78) "<?php var_dump("Second"); Phar::mapPhar("nouveau.phar"); __HALT_COMPILER(); ?>"

Ver también

add a note

User Contributed Notes 3 notes

up
1
Scott Dutton
7 years ago
Its not clear in the docs but __HALT_COMPILER() is required in the stub.
up
1
jaimz22 at gmail dot com
17 years ago
One thing I had alot of problems with, is that i can't set the stub unless I put the whole operation inside of a try/catch block!

If i remove the try/catch block it will error our and not write the stub with the content i want it to have.
up
0
Olivier Laviale
13 years ago
If your stub has a namespace, it is used for each include that doesn't define one.
To Top