略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoDB\BSON\Javascript::__construct

2024-12-22

MongoDB\BSON\Javascript::__construct

(mongodb >=1.0.0)

MongoDB\BSON\Javascript::__constructConstruct a new Javascript

说明

final public MongoDB\BSON\Javascript::__construct(string $code, array|object $scope = ?)

参数

code (string)

Javascript code.

scope (array|object)

Javascript scope.

错误/异常

更新日志

版本 说明
PECL mongodb 1.2.0

MongoDB\Driver\Exception\InvalidArgumentException is thrown if code contains null bytes. Previously, values would be truncated at the first null byte.

范例

示例 #1 MongoDB\BSON\Javascript::__construct() example

<?php

$code 
= new MongoDB\BSON\Javascript('function() { return 1; }');
var_dump($code);

$codews = new MongoDB\BSON\Javascript('function() { return foo; }', ['foo' => 'bar']);
var_dump($codews);

?>

以上例程会输出:

object(MongoDB\BSON\Javascript)#1 (2) {
  ["javascript"]=>
  string(24) "function() { return 1; }"
  ["scope"]=>
  object(stdClass)#2 (0) {
  }
}
object(MongoDB\BSON\Javascript)#2 (2) {
  ["javascript"]=>
  string(26) "function() { return foo; }"
  ["scope"]=>
  object(stdClass)#1 (1) {
    ["foo"]=>
    string(3) "bar"
  }
}
add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/mongodb-bson-javascript.construct.php

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