略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: V8Js::registerExtension

2024-11-13

V8Js::registerExtension

(PECL v8js >= 0.1.0)

V8Js::registerExtensionRegister Javascript extensions for V8Js

说明

public static V8Js::registerExtension(
    string $extension_name,
    string $script,
    array $dependencies = array(),
    bool $auto_enable = false
): bool

Registers passed Javascript script as extension to be used in V8Js contexts.

参数

extension_name

Name of the extension to be registered.

script

The Javascript code to be registered.

dependencies

Array of extension names the extension to be registered depends on. Any such extension is enabled automatically when this extension is loaded.

注意:

All extensions, including the dependencies, must be registered before any V8Js are created which use them.

auto_enable

If set to true, the extension will be enabled automatically in all V8Js contexts.

返回值

Returns true if extension was registered successfully, false otherwise.

add a noteadd a note

User Contributed Notes 2 notes

up
6
dimarikson at yandex dot ru
7 years ago
Usage sample:

if (V8Js::registerExtension('myjs', 'var x = 1 + 1;', array(), true) === false) {
    exit("Failed to register js extension script");
}

$v8js = new V8Js;

$jsExec = <<<EOD
x;
EOD;

echo $v8js->executeString($jsExec)."\n";    // print "2"
up
2
Reforced
2 years ago
Note that since version 2.0.0 V8Js::registerExtension is deprecated and suggests use snapshots instead https://github.com/phpv8/v8js/releases/tag/2.0.0
Simple example using snapshots and the moment.js:

<?php
$script
= file_get_contents('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js');

$snapshot = V8Js::createSnapshot($script);
$v8 = new V8Js('php', array(), array(), true, $snapshot);

echo
$v8->executeString('moment().format()');
?>

Side-note: If you value speed, security and stability do not use file_get_contents to grab external javascripts on production servers.

官方地址:https://www.php.net/manual/en/v8js.registerextension.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3