@@ -164,8 +164,32 @@ of reading the file.
164
164
Note that calls using ` Js\Wait ` block the PHP thread but do not
165
165
block the node thread.
166
166
167
+ ## class ` Js\ByRef `
168
+ Arguments are passed to JavaScript functions by value, as is the
169
+ default in PHP. This class allows you to pass arguments by reference;
170
+ specifically array values (since objects are effectively passed by
171
+ reference already, and it does not apply to primitive values like
172
+ strings and integers). Given the following JavaScript function
173
+ make available to PHP as ` $jsfunc ` :
174
+ ``` js
175
+ function jsfunc (arr ) {
176
+ Array .prototype .push .call (arr, 4 );
177
+ }
178
+ ```
179
+
180
+ You could call in from PHP as follows:
181
+ ``` php
182
+ $a = array(1, 2, 3);
183
+ $jsfunc($a);
184
+ var_dump($a); # would still print (1, 2, 3)
185
+
186
+ $jsfunc(new Js\ByRef($a));
187
+ var_dump($a); # now this would print (1, 2, 3, 4)
188
+ ```
189
+
167
190
# Javascript API
168
191
192
+ ## PHP objects
169
193
The JavaScript ` in ` operator, when applied to a wrapped PHP object,
170
194
works the same as the PHP [ ` isset() ` ] function. Similarly, when applied
171
195
to a wrapped PHP object, JavaScript ` delete ` works like PHP [ ` unset() ` ] .
@@ -214,6 +238,48 @@ php.request({
214
238
}).done ();
215
239
```
216
240
241
+ ## PHP arrays
242
+
243
+ PHP arrays are a sort of fusion of JavaScript arrays and objects.
244
+ They can store indexed data and have a sort of automatically-updated
245
+ ` length ` property, like JavaScript arrays, but they can also store
246
+ string keys like JavaScript objects.
247
+
248
+ In JavaScript, we've decided to expose arrays as
249
+ [ array-like] [ ` Map ` ] s.
250
+
251
+ That is, they have the
252
+ [ ` get ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get ) ,
253
+ [ ` set ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set ) ,
254
+ [ ` delete ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete ) ,
255
+ [ ` keys ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys ) , and
256
+ [ ` size ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size )
257
+ methods of [ ` Map ` ] . These work as you'd expect, and access * all* the
258
+ values in the PHP array, with both indexed and string keys.
259
+
260
+ In addition, as a convenience, they make the * indexed* keys (and only
261
+ the indexed keys) available as properties directly on the object,
262
+ and export an appropriate ` length ` field. This lets you use them
263
+ directly in many JavaScript functions which accept "array-like"
264
+ objects. For example, you can convert them easily to a "true"
265
+ JavaScript array with [ ` Array.from ` ] .
266
+
267
+ Arrays like objects are live-mapped: changes apply directly to
268
+ the PHP object they wrap. However, note that arrays are by default
269
+ passed * by value* to JavaScript functions; you may need to
270
+ use ` Js\ByRef ` (see above) in order to have changes you make
271
+ on the JavaScript side affect the value of a PHP variable.
272
+
273
+ ## PHP ArrayAccess/Countable
274
+ PHP objects which implement [ ` ArrayAccess ` ] and [ ` Countable ` ] are treated
275
+ as PHP arrays, with the accessor methods described above. However
276
+ note that the ` length ` property is fixed to ` 0 ` on these objects,
277
+ since there's no way to get a count of only the indexed keys
278
+ in the array ([ ` Countable ` ] gives the count of * all* the keys,
279
+ counting both indexed and string keys).
280
+
281
+ ## Blocking the JavaScript event loop
282
+
217
283
At the moment, all property accesses and method invocations from
218
284
JavaScript to PHP are done synchronously; that is, they block the
219
285
JavaScript event loop. The mechanisms are in place for asynchronous
@@ -353,6 +419,12 @@ Copyright (c) 2015 C. Scott Ananian.
353
419
[ `fs.readFile` ] : https://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback
354
420
[ `isset()` ] : http://php.net/manual/en/function.isset.php
355
421
[ `unset()` ] : http://php.net/manual/en/function.unset.php
422
+ [ `ArrayAccess` ] : http://php.net/manual/en/class.arrayaccess.php
423
+ [ `Countable` ] : http://php.net/manual/en/class.countable.php
424
+
425
+ [ array-like ] : http://www.2ality.com/2013/05/quirk-array-like-objects.html
426
+ [ `Map` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
427
+ [ `Array.from` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
356
428
357
429
[ NPM1 ] : https://nodei.co/npm/php-embed.png
358
430
[ NPM2 ] : https://nodei.co/npm/php-embed/
0 commit comments