Skip to content

Commit 7183c10

Browse files
committed
Update README and CHANGELOG.
1 parent 41dae76 commit 7183c10

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
# php-embed x.x.x (not yet released)
2+
* Ensure PHP private properties aren't writable.
3+
* Implement `__toString` magic method on wrapped JavaScript objects.
4+
* Include stack trace when JavaScript exceptions are thrown into PHP.
5+
* Implement wrapping of PHP arrays and objects implementing
6+
`ArrayAccess` and `Countable`.
7+
* Add `Js\ByRef` to allow arrays to be passed by reference to
8+
JavaScript functions.
9+
* Add binaries for node 5.0.0.
210

311
# php-embed 0.5.1 (2015-10-29)
412
* Support passing cookies and POST data to PHP request.

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,32 @@ of reading the file.
164164
Note that calls using `Js\Wait` block the PHP thread but do not
165165
block the node thread.
166166

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+
167190
# Javascript API
168191

192+
## PHP objects
169193
The JavaScript `in` operator, when applied to a wrapped PHP object,
170194
works the same as the PHP [`isset()`] function. Similarly, when applied
171195
to a wrapped PHP object, JavaScript `delete` works like PHP [`unset()`].
@@ -214,6 +238,48 @@ php.request({
214238
}).done();
215239
```
216240

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+
217283
At the moment, all property accesses and method invocations from
218284
JavaScript to PHP are done synchronously; that is, they block the
219285
JavaScript event loop. The mechanisms are in place for asynchronous
@@ -353,6 +419,12 @@ Copyright (c) 2015 C. Scott Ananian.
353419
[`fs.readFile`]: https://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback
354420
[`isset()`]: http://php.net/manual/en/function.isset.php
355421
[`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
356428

357429
[NPM1]: https://nodei.co/npm/php-embed.png
358430
[NPM2]: https://nodei.co/npm/php-embed/

0 commit comments

Comments
 (0)