PHP Conference Fukuoka 2025

Phar::mapPhar

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

Phar::mapPharLee el phar ejecutado y carga su manifiesto

Descripción

final public static Phar::mapPhar(?string $alias = null, int $offset = 0): bool

Este método estático puede ser utilizado únicamente dentro del contenedor de carga de un archivo Phar para inicializar el phar cuando es ejecutado directamente o cuando es incluido en otro script.

Parámetros

alias

El alias que puede ser utilizado en la URL phar:// para referirse al archivo en lugar de utilizar su ruta completa.

offset

Variable no utilizada, presente por motivos de compatibilidad con la biblioteca PHP_Archive de PEAR.

Valores devueltos

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

Errores/Excepciones

Se lanza una excepción PharException si el método no es llamado directamente dentro de la ejecución de PHP, si no se encuentra ningún token __HALT_COMPILER(); en el archivo fuente actual o si el archivo no puede ser abierto en lectura.

Ejemplos

Ejemplo #1 Ejemplo con Phar::mapPhar()

mapPhar debe ser utilizado únicamente dentro del contenedor de carga de un phar. Utilice loadPhar para cargar un phar externo en memoria.

A continuación se muestra un ejemplo de contenedor de carga Phar que utiliza mapPhar.

<?php
function __autoload($class)
{
include
'phar://mon.phar/' . str_replace('_', '/', $class) . '.php';
}
try {
Phar::mapPhar('mon.phar');
include
'phar://mon.phar/demarrage.php';
} catch (
PharException $e) {
echo
$e->getMessage();
die(
'No puede inicializar el Phar');
}
__HALT_COMPILER();

Ver también

add a note

User Contributed Notes 1 note

up
1
phofstetter at sensational dot ch
12 years ago
Be careful with mapPhar and opcode caches like opcache: They might cache files included by the symbolic name based on the symbolic name you give.This becomes a problem when a server is hosting multiple different versions of a phar file all using the same symbolic name because then subsequent include()'s in the phar file might load an already cached file from another version of the phar file.Instead, generate a unique name and use that in mapPhar and in subsequent include()'sSee for example https://github.com/zendtech/ZendOptimizerPlus/issues/115#issuecomment-25612769 for the issue in the opcache module.
To Top