<?xml version="1.0" encoding="utf-8"?>

<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  xmlns:admin="http://webns.net/mvcb/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:cc="http://web.resource.org/cc/"
  xmlns="http://purl.org/rss/1.0/">

<channel rdf:about="http://developer.cybozu.co.jp/tech/">
<title>Cybozu Inside Out</title>
<link>http://developer.cybozu.co.jp/tech/</link>
<description></description>
<dc:language>ja-JP</dc:language>
<dc:creator></dc:creator>
<dc:date>2010-02-04T13:16:32+09:00</dc:date>
<admin:generatorAgent rdf:resource="http://www.typepad.com/" />


<items>
<rdf:Seq><rdf:li rdf:resource="http://developer.cybozu.co.jp/tech/2010/02/spidermonkeyphp.html" />
<rdf:li rdf:resource="http://developer.cybozu.co.jp/tech/2010/01/post-ffce.html" />
<rdf:li rdf:resource="http://developer.cybozu.co.jp/tech/2009/12/cybozu-toolbar.html" />
<rdf:li rdf:resource="http://developer.cybozu.co.jp/tech/2009/10/2-27b8.html" />
<rdf:li rdf:resource="http://developer.cybozu.co.jp/tech/2009/09/post-6905.html" />
</rdf:Seq>
</items>

</channel>

<item rdf:about="http://developer.cybozu.co.jp/tech/2010/02/spidermonkeyphp.html">
<title>SpiderMonkeyを使ってPHPでサーバーサイドJavaScript</title>
<link>http://developer.cybozu.co.jp/tech/2010/02/spidermonkeyphp.html</link>
<description>はじめまして。サイボウズのあまちゃんです！

先日 SpiderMonkey を利用して PHP から JavaScript を実行する方法を調べる機会がありましたので、ご紹介します。</description>
<content:encoded>&lt;p&gt;はじめまして。2009年に新卒で入社しました天野祐介です。amachang を期待された方はゴメンナサイ！&lt;/p&gt;

&lt;p&gt;先日 SpiderMonkey を利用して PHP から JavaScript を実行する方法を調べる機会がありましたので、ご紹介します。&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SpiderMonkey とは&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SpiderMonkey は&amp;nbsp; C で実装された Mozilla の JavaScript エンジンです。 これを PHP から実行する拡張を利用すると、 PHP コード内で JavaScript が実行できます。&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SpiderMonkey extension のインストール&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;こちら&lt;a href=&quot;http://devzone.zend.com/article/4704&quot;&gt;http://devzone.zend.com/article/4704&lt;/a&gt;に記載されている方法で CentOS にインストールしてみました。&lt;br /&gt;PHP 5.3.0 以上が必要です。&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;tar -xzvf js-1.70.tar-gz
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;js/src
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make -f Makefile.ref
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;mkdir -p /usr/local/include/js/
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cp *.&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;h,tbl&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; /usr/local/include/js/
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;Linux_All_DBG.OBJ/
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cp *.h /usr/local/include/js/
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cp js /usr/local/bin/
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;cp libjs.so /usr/local/lib/
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;/sbin/ldconfig
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;svn &lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;https://ookoo.org/svn/pecl-spidermonkey/ ./sm
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;sm
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;phpize
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./configure
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make install
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20090626/
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;インストール先を確認して、 php.ini に以下の行を追加します。&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;nv&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;/usr/local/lib/php/extensions/no-debug-non-zts-20090626/spidermonkey.so&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;それでは動かしてみましょう！&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hello, World&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// jsでPHPの関数を使えるようにする&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;var_dump&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// jsコードの定義&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hello&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, World!!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// jsコードを評価&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;実行結果&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;Hello, World!!&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;JavaScript から var_dump() を実行してコンソールに出力しています。&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSでPHPのクラス, 関数を使う&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;var_dump() 以外にも、 JavaScript から PHP のクラスや関数を使ってみます。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;myclass.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;hoge&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$foo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;abc&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;def&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$bar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;dog&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;wanwan&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;s1&quot;&gt;&amp;#39;cat&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;nya-!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;!!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;call.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;require_once&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;myclass.php&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$hoge&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hoge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// PHPの関数, クラスを使えるようにする&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;var_dump&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hoge&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;hoge&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// PHPの変数を使えるようにする&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;assign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hogedog&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hoge&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$bar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;dog&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// define script&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hoge&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hoge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hogedog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hoge&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hoge&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hoge&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;amano&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;実行結果&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;# php -f call.php&lt;br /&gt;string(6) &amp;quot;wanwan&amp;quot;&lt;br /&gt;object(stdClass)#4 (2) {&lt;br /&gt;&amp;nbsp; [&amp;quot;0&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; string(3) &amp;quot;abc&amp;quot;&lt;br /&gt;&amp;nbsp; [&amp;quot;1&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; string(3) &amp;quot;def&amp;quot;&lt;br /&gt;}&lt;br /&gt;int(25)&lt;br /&gt;string(14) &amp;quot;Hello, amano!!&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;配列もちゃんと出力できました。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSで定義したオブジェクト, 関数を使う&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;次は、 JavaScript のオブジェクトを PHP から触ってみましょう。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;jsobj.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;var_dump&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// タイムゾーンを設定&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;date_default_timezone_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Asia/Tokyo&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;// 現在の時（24時間）を取得&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$hour&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;H&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// assign&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;assign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;time&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$hour&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// define script&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$script1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;menu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;morning&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ばなな&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;lunch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;sandwich&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dinner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;steak&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;menu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$script2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;menu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;morning&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ばなな&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;lunch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;sandwich&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dinner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;steak&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;gohan&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;okazu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;menu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;morning&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;menu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;lunch&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;menu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;dinner&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;nx&quot;&gt;gohan&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;okazu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;今日のおかずは %s よ！&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;実行結果&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;# php -f jsobj.php
object(stdClass)#2 (3) {
  [&quot;morning&quot;]=&gt;
  string(9) &quot;ばなな&quot;
  [&quot;lunch&quot;]=&gt;
  string(8) &quot;sandwich&quot;
  [&quot;dinner&quot;]=&gt;
  string(5) &quot;steak&quot;
}
今日のおかずは steak よ！&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;evaluateScript() メソッドは最後に評価した値を返します。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;外部 js ファイルを使う&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;以下の js ファイルを読み込んで実行してみます。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function.js&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;getYearMonth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;date&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getFullYear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;/&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getMonth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;main.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$library&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;file_get_contents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;function.js&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getYearMonth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;実行結果&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;string(6) &amp;quot;2010/2&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;思いのほか簡単に動きました！ユーティリティ関数を外部 js の中に詰め込んでおけば、 PHP と JavaScript で共有できそうですね。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PHP に値を渡した時の挙動&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript から PHP にオブジェクトを渡した時の型の変化をもう少し詳しく見てみましょう。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;var_dump&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;array_func&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;array_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;get_object_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;実行結果&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;int(5)&lt;br /&gt;array(2) {&lt;br /&gt;&amp;nbsp; [0]=&amp;gt;&lt;br /&gt;&amp;nbsp; int(1)&lt;br /&gt;&amp;nbsp; [4]=&amp;gt;&lt;br /&gt;&amp;nbsp; int(5)&lt;br /&gt;}&lt;br /&gt;int(2)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;返り値の array は StdClass に包まれていたので、 get_object_vars() で取り出しています。&lt;/p&gt;

&lt;p&gt;JavaScript は array[1]〜array[3] に &amp;quot;undefined&amp;quot; が入り長さは 5 ですが、PHP では長さ 2 となります。&lt;br /&gt;
PHP で存在しないインデックス（array[1]〜array[3]）にアクセスすると、 NULL が返ります。
&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;実行結果&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;object(stdClass)#2 (1) {&lt;br /&gt;&amp;nbsp; [&amp;quot;prototype&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; object(stdClass)#3 (0) {&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Function は空のオブジェクトになりました。&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;実数&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Math オブジェクトや Number オブジェクトに含まれる実数値を PHP に渡して出力してみました。&lt;br /&gt;コードは割愛します。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Math.PI&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;js&amp;nbsp; -&amp;gt; 3.141592653589793&lt;br /&gt;php -&amp;gt; 3.1415926535898&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Number.MAX_VALUE&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;js&amp;nbsp; -&amp;gt; 1.7976931348623157e+308&lt;br /&gt;php -&amp;gt; 1.7976931348623e+308&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Number.MIN_VALUE&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;js&amp;nbsp; -&amp;gt; 5e-324&lt;br /&gt;php -&amp;gt; 4.9406564584125e-324&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;浮動小数点数は異なる精度で出力されてしまいました。 PHP に実数を渡す場合は、 BC Math 関数&lt;a href=&quot;http://www.php.net/manual/ja/ref.bc.php&quot;&gt;http://www.php.net/manual/ja/ref.bc.php&lt;/a&gt;などを使う必要がありそうです。&lt;br /&gt;ちなみに、JavaScriptの未定義値 &amp;quot;undefined&amp;quot; をPHPに渡したら NULL になりました。 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;連想配列&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;var_dump&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;aiueo&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}};&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;get_object_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;実行結果&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;array(4) {&lt;br /&gt;&amp;nbsp; [&amp;quot;num&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; int(1)&lt;br /&gt;&amp;nbsp; [&amp;quot;str&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; string(5) &amp;quot;aiueo&amp;quot;&lt;br /&gt;&amp;nbsp; [&amp;quot;array&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; object(stdClass)#3 (2) {&lt;br /&gt;&amp;nbsp; &amp;nbsp; [&amp;quot;0&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; int(1)&lt;br /&gt;&amp;nbsp; &amp;nbsp; [&amp;quot;1&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; int(2)&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;&amp;nbsp; [&amp;quot;func&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; object(stdClass)#4 (1) {&lt;br /&gt;&amp;nbsp; &amp;nbsp; [&amp;quot;prototype&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; object(stdClass)#5 (0) {&lt;br /&gt;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;PHP でも連想配列として扱うことができました。 関数オブジェクトはやはり空のオブジェクトになってしまいますが。。。&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PHP に値を渡したときの挙動まとめ&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScriptが 数値, 文字列, 論理値, null, undefined, NaN 以外の値を返す場合、PHPには StdClass オブジェクトとして渡される。&lt;/li&gt;
&lt;li&gt;配列や連想配列は get_object_vars() で取り出せる&lt;/li&gt;
&lt;li&gt;関数オブジェクトは PHP に渡せない&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;エラー処理&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript コード内でのエラーの扱いを見てみます。&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;registerFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;var_dump&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;hoge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;// 未定義関数&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$js&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;evaluateScript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;実行結果&lt;/strong&gt;&lt;/p&gt;

&lt;pre style=&quot;border: 1px solid ; padding: 6px;&quot;&gt;&lt;code&gt;object(stdClass)#2 (4) {&lt;br /&gt;&amp;nbsp; [&amp;quot;message&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; string(19) &amp;quot;hoge is not defined&amp;quot;&lt;br /&gt;&amp;nbsp; [&amp;quot;fileName&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; string(0) &amp;quot;&amp;quot;&lt;br /&gt;&amp;nbsp; [&amp;quot;lineNumber&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; int(1)&lt;br /&gt;&amp;nbsp; [&amp;quot;stack&amp;quot;]=&amp;gt;&lt;br /&gt;&amp;nbsp; string(4) &amp;quot;@:1&lt;br /&gt;&amp;quot;&lt;br /&gt;}&lt;br /&gt;セグメンテーション違反です&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;var_dump() との折り合いが悪いのかセグメンテーション違反が発生してしまいましたが、エラーオブジェクトのプロパティは正しく参照できます。&lt;br /&gt;e.linenumber は 0 から始まり、エラーの発生した行が格納されています。&lt;/p&gt;

&lt;p&gt;e.linenumber でエラー行が分かるのは便利ですが、 ひとつの PHP コード内で 複数回 evalueateScript() を実行したときに、どの evaluateScript() での行番号か特定できないという問題があります。&lt;br /&gt;この問題は後述のパッチを適用することで回避可能です。パッチを適用すると、 evaluateScript() の第2引数に e.fileName に出力される名前を設定できるようになります。&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;まとめ&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;
いかがでしたでしょうか。PHP と JavaScript でお互いの関数やオブジェクトを繰り返し実行すると結構バギーな動きをするので安全とは言い難いですが、 共通の処理を JavaScript のコードにまとめられたり JavaScript からサーバーサイドのオブジェクトが操作できるのは魅力的だと思います。JavaScript の活躍する場所はクライアントサイドだけじゃないということで、何かのご参考になれば幸いです。
&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;パッチ情報&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;こちらでラボの星野さんが作成されたパッチを公開しています。&lt;br /&gt;&lt;a href=&quot;http://developer.cybozu.co.jp/oss/2010/01/spidermonkey-ph.html&quot;&gt;http://developer.cybozu.co.jp/oss/2010/01/spidermonkey-ph.html&lt;/a&gt;&lt;br /&gt;windows build のサポート，evaluateScript() の引数の追加，報告されたバグの改修の3点です。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;参考にさせていただいたページ&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.mozilla.org/js/spidermonkey/&quot;&gt;http://www.mozilla.org/js/spidermonkey/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://pecl.php.net/package/spidermonkey&quot;&gt;http://pecl.php.net/package/spidermonkey&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://devzone.zend.com/article/4704&quot;&gt;http://devzone.zend.com/article/4704&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://d.hatena.ne.jp/shimooka/20090309/1236579091&quot;&gt;http://d.hatena.ne.jp/shimooka/20090309/1236579091&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;</content:encoded>



<dc:creator>admin</dc:creator>
<dc:date>2010-02-04T13:16:32+09:00</dc:date>
</item>
<item rdf:about="http://developer.cybozu.co.jp/tech/2010/01/post-ffce.html">
<title>新年明けましておめでとうございます</title>
<link>http://developer.cybozu.co.jp/tech/2010/01/post-ffce.html</link>
<description>新年明けましておめでとうございます。 昨年は百年に一度の大不況、政権交代、円高、デフレと企業はもとより個人にとってもつらい年となりました。 しかし、私たちはそんな大不況を吹き飛ばしたい思いから、家族、...</description>
<content:encoded>&lt;p&gt;新年明けましておめでとうございます。 &lt;br /&gt;
昨年は百年に一度の大不況、政権交代、円高、デフレと企業はもとより個人にとってもつらい年となりました。&lt;/p&gt;

&lt;p&gt;しかし、私たちはそんな大不況を吹き飛ばしたい思いから、家族、友人、ビジネスのコミュニケーションを活性化させる&lt;a href=&quot;http://live.cybozu.co.jp/&quot;&gt;無料グループウェア、「サイボウズ Live&lt;/a&gt;&lt;a href=&quot;http://live.cybozu.co.jp/&quot;&gt;」&lt;/a&gt;を公開しました。現在、招待制で運用を開始しており、一般登録制に向けて準備中です。 &lt;br /&gt;
こちら&lt;a rel=&quot;nofollow&quot; class=&quot;external-link&quot; href=&quot;http://blog.cybozulive.com/&quot;&gt;http://blog.cybozulive.com/&lt;/a&gt;のブログにて情報を発信しております。 &lt;/p&gt;

&lt;p&gt;今年はOffice、ガルーン２のバージョンアップだけでなく、マイクロソフト社との業務提携によるSharepoint向けのグループウェアを発売致します。こちらは従来のグループウェアの新バージョンではなく、全く新しい製品ラインナップになります。 &lt;br /&gt;
こちら&lt;a rel=&quot;nofollow&quot; class=&quot;external-link&quot; href=&quot;http://group.cybozu.jp/news/09092801.html&quot;&gt;http://group.cybozu.jp/news/09092801.html&lt;/a&gt;がプレスリリースになります。&lt;/p&gt;

&lt;p&gt;また、サイボウズとサイボウズラボとのコラボレーションによる次世代グループウェアR&amp;amp;Dも昨年より開始しています。ご期待ください。 &lt;/p&gt;

&lt;p&gt;その他にもワクワクする製品を今年いくつかリリース致します。 &lt;br /&gt;
本年もサイボウズとサイボウズラボを宜しくお願いします。 &lt;/p&gt;</content:encoded>



<dc:creator>admin</dc:creator>
<dc:date>2010-01-13T08:48:52+09:00</dc:date>
</item>
<item rdf:about="http://developer.cybozu.co.jp/tech/2009/12/cybozu-toolbar.html">
<title>Cybozu Toolbar</title>
<link>http://developer.cybozu.co.jp/tech/2009/12/cybozu-toolbar.html</link>
<description>サイボウズ開発部でアルバイトをしている郷原浩之です。 サイボウズ開発部では3人のアルバイトと一緒に、サイボウズOfficeへの直感的なアクセスを可能にするためのブラウザツールバーを作っております。 h...</description>
<content:encoded>&lt;p&gt;サイボウズ開発部でアルバイトをしている郷原浩之です。 &lt;/p&gt;

&lt;p&gt;サイボウズ開発部では3人のアルバイトと一緒に、サイボウズOfficeへの直感的なアクセスを可能にするためのブラウザツールバーを作っております。 &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://kantan.cybozu.co.jp/office7/option/client/toolbar/ &quot;&gt;http://kantan.cybozu.co.jp/office7/option/client/toolbar/ &lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;現在4ヶ月目になりますが、開始当初に比べてより使いやすい機能が幾つか追加できたかと思います。ドキュメントなどが少なく、またCOMやATLの知識が乏しいところからのスタートだったので、当初はかなり苦労しました。 &lt;/p&gt;

&lt;p&gt;サイボウズではサイボウズガルーン（グループウェアソフト）を自社で稼動させていますが、そこに疑問点を書いておくと、上級者の方がコメントをくださり、とても心強いです。現在は、サイボウズツールバーのFireFoxへの移植を行っておりますが、javascript等でのアドバイスについて、サイボウズガルーンに書き込んでいただき助かっております。&lt;/p&gt;</content:encoded>



<dc:creator>admin</dc:creator>
<dc:date>2009-12-04T14:37:55+09:00</dc:date>
</item>
<item rdf:about="http://developer.cybozu.co.jp/tech/2009/10/2-27b8.html">
<title>第2回技術発表会を行ないました</title>
<link>http://developer.cybozu.co.jp/tech/2009/10/2-27b8.html</link>
<description>こんにちは、サイボウズ篠原です。 夏も終わりそろそろ肌寒くなってきましたね。 9月の頭の話になりますが、社内で技術発表会を開催しました。 この催しは開発部の技術交流を目的としており、 各拠点とグループ...</description>
<content:encoded>&lt;p&gt;&lt;a onclick=&quot;window.open(this.href, &#39;_blank&#39;, &#39;width=800,height=600,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0&#39;); return false&quot; href=&quot;http://developer.cybozu.co.jp/.shared/image.html?/photos/uncategorized/2009/10/02/dscf6235.jpg&quot;&gt;&lt;img width=&quot;300&quot; height=&quot;225&quot; border=&quot;0&quot; src=&quot;http://developer.cybozu.co.jp/tech/images/2009/10/02/dscf6235.jpg&quot; title=&quot;Dscf6235&quot; alt=&quot;Dscf6235&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;こんにちは、サイボウズ篠原です。&lt;br /&gt;夏も終わりそろそろ肌寒くなってきましたね。&lt;br /&gt;9月の頭の話になりますが、社内で技術発表会を開催しました。&lt;/p&gt;

&lt;p&gt;この催しは開発部の技術交流を目的としており、&lt;br /&gt;各拠点とグループ会社であるサイボウズラボとサイボウズ総研に&lt;br /&gt;参加いただいています。&lt;/p&gt;

&lt;p&gt;この技術発表会、今回で2度目となり、&lt;br /&gt;開発、運用のノウハウ、そして新プロジェクト、と&lt;br /&gt;目白押しの内容。&lt;br /&gt;メンバーにとってよい刺激になっています。&lt;/p&gt;

&lt;p&gt;残念ながら対外的に出せない情報が多いですが&lt;br /&gt;タイトルだけちょっと公開しようと思います。&lt;br /&gt;&lt;span style=&quot;color: #cc0033;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a onclick=&quot;window.open(this.href, &#39;_blank&#39;, &#39;width=800,height=600,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0&#39;); return false&quot; href=&quot;http://developer.cybozu.co.jp/.shared/image.html?/photos/uncategorized/2009/10/02/dscf6229_2.jpg&quot;&gt;&lt;img width=&quot;300&quot; height=&quot;225&quot; border=&quot;0&quot; src=&quot;http://developer.cybozu.co.jp/tech/images/2009/10/02/dscf6229_2.jpg&quot; title=&quot;Dscf6229_2&quot; alt=&quot;Dscf6229_2&quot; /&gt;&lt;/a&gt; &lt;/p&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff0033;&quot;&gt;&lt;strong&gt;本社開発部&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;Gaia (応用発展系アプリ)&lt;br /&gt;Mercury (モバイル系調査)&lt;br /&gt;グループウェアネットサービス&lt;/p&gt;

&lt;p&gt;&lt;a onclick=&quot;window.open(this.href, &#39;_blank&#39;, &#39;width=800,height=600,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0&#39;); return false&quot; href=&quot;http://developer.cybozu.co.jp/.shared/image.html?/photos/uncategorized/2009/10/02/dscf6231.jpg&quot;&gt;&lt;img width=&quot;300&quot; height=&quot;225&quot; border=&quot;0&quot; src=&quot;http://developer.cybozu.co.jp/tech/images/2009/10/02/dscf6231.jpg&quot; title=&quot;Dscf6231&quot; alt=&quot;Dscf6231&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: #ff0033;&quot;&gt;&lt;strong&gt;サイボウズラボ&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;AzDz by Windows Azure&lt;br /&gt;Fast IMAP Proxy&lt;br /&gt;Key-Value Storage Mio&lt;br /&gt;A Clever Way to Scale-out a Web Application&lt;br /&gt;Application Development on SharePoint&lt;br /&gt;Lab&#39;s VM Environment&lt;br /&gt;Bamboo Project Report&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a onclick=&quot;window.open(this.href, &#39;_blank&#39;, &#39;width=800,height=600,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0&#39;); return false&quot; href=&quot;http://developer.cybozu.co.jp/.shared/image.html?/photos/uncategorized/2009/10/02/dscf6234.jpg&quot;&gt;&lt;img width=&quot;300&quot; height=&quot;225&quot; border=&quot;0&quot; src=&quot;http://developer.cybozu.co.jp/tech/images/2009/10/02/dscf6234.jpg&quot; title=&quot;Dscf6234&quot; alt=&quot;Dscf6234&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: #ff0033;&quot;&gt;&lt;strong&gt;サイボウズ総研&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;Garoon2 EC2&lt;br /&gt;Garoon2 IP Phone Connection&lt;br /&gt;Garoon2 Exchange Connection&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;a onclick=&quot;window.open(this.href, &#39;_blank&#39;, &#39;width=800,height=600,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0&#39;); return false&quot; href=&quot;http://developer.cybozu.co.jp/.shared/image.html?/photos/uncategorized/2009/10/02/dscf6226.jpg&quot;&gt;&lt;img width=&quot;300&quot; height=&quot;225&quot; border=&quot;0&quot; src=&quot;http://developer.cybozu.co.jp/tech/images/2009/10/02/dscf6226.jpg&quot; title=&quot;Dscf6226&quot; alt=&quot;Dscf6226&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span style=&quot;color: #ff0033;&quot;&gt;&lt;strong&gt;情報システム部&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;内部統制&lt;br /&gt;オフショア&lt;br /&gt;ストレージ研究&lt;br /&gt;バックアップシステム&lt;br /&gt;顧客情報システム&lt;br /&gt;Silverlight&lt;/p&gt;

&lt;p&gt;松山開発部の活動&lt;br /&gt;上海開発部の紹介&lt;/p&gt;

&lt;p&gt;次回の技術発表会は3月を予定しています。

&lt;/p&gt;

&lt;p&gt;もしかすると対外的に出せる情報も増えるかもしれませんし&lt;br /&gt;新しいプロジェクトがはじまっているかもしれません。&lt;br /&gt;お楽しみに。&lt;/p&gt;</content:encoded>


<dc:subject>Cybozu</dc:subject>

<dc:creator>admin</dc:creator>
<dc:date>2009-10-05T10:51:19+09:00</dc:date>
</item>
<item rdf:about="http://developer.cybozu.co.jp/tech/2009/09/post-6905.html">
<title>ものづくり日本大賞受賞</title>
<link>http://developer.cybozu.co.jp/tech/2009/09/post-6905.html</link>
<description>サイボウズ松山の矢野です。 いまさら感の否めないご報告となりますが、 去る 平成21年7月某日、件の賞を頂戴いたしました。 第18回の高専プロコンへ出展した作品が評価されたとのことで、 当時の開発メン...</description>
<content:encoded>&lt;p&gt;サイボウズ松山の矢野です。&lt;/p&gt;
&lt;p&gt;いまさら感の否めないご報告となりますが、&lt;br /&gt;去る 平成21年7月某日、件の賞を頂戴いたしました。&lt;/p&gt;
&lt;p&gt;&lt;img height=&quot;200&quot; width=&quot;300&quot; border=&quot;0&quot; src=&quot;http://developer.cybozu.co.jp/tech/images/2009/09/25/photo_2.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;第18回の高専プロコンへ出展した作品が評価されたとのことで、&lt;br /&gt;当時の開発メンバーが一同に集い、あたかも学生時代に戻ったかのようでした。&lt;/p&gt;
&lt;p&gt;実感の湧かぬまま表彰式を迎え、終えたので、未だに夢か幻のような気分であります。&lt;br /&gt;首相官邸にて賞を賜ったのですが、瀟洒な雰囲気の中に身を置くにあたり、場違いな印象を受けるばかりでした。&lt;br /&gt;官邸の天井は高かったです。&lt;/p&gt;
&lt;p&gt;第92代首相の力強い握手と励ましのお言葉を忘れません。&lt;br /&gt;この熱い想いを胸に、製品開発に全力を注いでゆきたいと思います。&lt;/p&gt;
&lt;p&gt;&lt;img height=&quot;180&quot; width=&quot;300&quot; border=&quot;0&quot; src=&quot;http://developer.cybozu.co.jp/tech/images/2009/09/25/medal.jpg&quot; /&gt;&lt;/p&gt;
&lt;p&gt;関連URL：&lt;br /&gt;
&lt;a href=&quot;http://www.monodzukuri.meti.go.jp/&quot;&gt;[第3回ものづくり日本大賞]&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://cydn.cybozu.co.jp/2008/10/programmers_in_matsuyama.html&quot;&gt;[Cydn - 地方でプログラマは育つのか]&lt;/a&gt;&lt;/p&gt;</content:encoded>


<dc:subject>Cybozu</dc:subject>

<dc:creator>松山開発部</dc:creator>
<dc:date>2009-09-25T20:19:50+09:00</dc:date>
</item>


</rdf:RDF>
