Modified: websites/staging/thrift/trunk/content/tutorial/php.html ============================================================================== --- websites/staging/thrift/trunk/content/tutorial/php.html (original) +++ websites/staging/thrift/trunk/content/tutorial/php.html Sat Mar 22 18:57:03 2014 @@ -87,234 +87,189 @@ </ol> <h3 id="prerequisites">Prerequisites</h3> <h3 id="client">Client</h3> -<div class="codehilite"><pre> <span class="p">:::</span><span class="n">php</span> -<span class="o">&</span><span class="n">lt</span><span class="p">;</span>?<span class="n">php</span> - -<span class="n">namespace</span> <span class="n">tutorial</span><span class="o">\</span><span class="n">php</span><span class="p">;</span> - -<span class="n">error_reporting</span><span class="p">(</span><span class="n">E_ALL</span><span class="p">);</span> - -<span class="n">require_once</span> <span class="n">__DIR__</span><span class="p">.</span><span class="o">&</span>#39<span class="p">;</span><span class="o">/</span><span class="p">.</span><span class="o">./</span><span class="p">.</span><span class="o">./</span><span class="n">lib</span><span class="o">/</span><span class="n">php</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">Thrift</span><span class="o">/</span><span class="n">ClassLoader</span><span class="o">/</span><span class="n">ThriftClassLoader</span><span class="p">.</span><span class="n">php</span><span class="o">&</span>#39<span class="p">;;</span> - -<span class="n">use</span> <span class="n">Thrift</span><span class="o">\</span><span class="n">ClassLoader</span><span class="o">\</span><span class="n">ThriftClassLoader</span><span class="p">;</span> - -$<span class="n">GEN_DIR</span> <span class="p">=</span> <span class="n">realpath</span><span class="p">(</span><span class="n">dirname</span><span class="p">(</span><span class="n">__FILE__</span><span class="p">).</span><span class="o">&</span>#39<span class="p">;</span><span class="o">/</span><span class="p">..</span><span class="o">&</span>#39<span class="p">;).</span><span class="o">&</span>#39<span class="p">;</span><span class="o">/</span><span class="n">gen</span><span class="o">-</span><span class="n">php</span><span class="o">&</span>#39<span class="p">;;</span> - -$<span class="n">loader</span> <span class="p">=</span> <span class="n">new</span> <span class="n">ThriftClassLoader</span><span class="p">();</span> -$<span class="n">loader</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">registerNamespace</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;</span><span class="n">Thrift</span><span class="o">&</span>#39<span class="p">;,</span> <span class="n">__DIR__</span> <span class="p">.</span> <span class="o">&</span>#39<span class="p">;</span><span class="o">/</span><span class="p">.</span><span class="o">./</span><span class="p">.</span><span class="o">./</span><span class="n">lib</span><span class="o">/</span><span class="n">php</span><span class="o">/</span><span class="n">lib</span><span class="o">&</span>#39<span class="p">;);</span> -$<span class="n">loader</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">registerDefinition</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;</span><span class="n">shared</span><span class="o">&</span>#39<span class="p">;,</span> $<span class="n">GEN_DIR</span><span class="p">);</span> -$<span class="n">loader</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">registerDefinition</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;</span><span class="n">tutorial</span><span class="o">&</span>#39<span class="p">;,</span> $<span class="n">GEN_DIR</span><span class="p">);</span> -$<span class="n">loader</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">register</span><span class="p">();</span> - -<span class="o">/*</span> - <span class="o">*</span> <span class="n">Licensed</span> <span class="n">to</span> <span class="n">the</span> <span class="n">Apache</span> <span class="n">Software</span> <span class="n">Foundation</span> <span class="p">(</span><span class="n">ASF</span><span class="p">)</span> <span class="n">under</span> <span class="n">one</span> - <span class="o">*</span> <span class="n">or</span> <span class="n">more</span> <span class="n">contributor</span> <span class="n">license</span> <span class="n">agreements</span><span class="p">.</span> <span class="n">See</span> <span class="n">the</span> <span class="n">NOTICE</span> <span class="n">file</span> - <span class="o">*</span> <span class="n">distributed</span> <span class="n">with</span> <span class="n">this</span> <span class="n">work</span> <span class="k">for</span> <span class="n">additional</span> <span class="n">information</span> - <span class="o">*</span> <span class="n">regarding</span> <span class="n">copyright</span> <span class="n">ownership</span><span class="p">.</span> <span class="n">The</span> <span class="n">ASF</span> <span class="n">licenses</span> <span class="n">this</span> <span class="n">file</span> - <span class="o">*</span> <span class="n">to</span> <span class="n">you</span> <span class="n">under</span> <span class="n">the</span> <span class="n">Apache</span> <span class="n">License</span><span class="p">,</span> <span class="n">Version</span> 2<span class="p">.</span>0 <span class="p">(</span><span class="n">the</span> - <span class="o">*</span> <span class="o">&</span><span class="n">quot</span><span class="p">;</span><span class="n">License</span><span class="o">&</span><span class="n">quot</span><span class="p">;);</span> <span class="n">you</span> <span class="n">may</span> <span class="n">not</span> <span class="n">use</span> <span class="n">this</span> <span class="n">file</span> <span class="n">except</span> <span class="n">in</span> <span class="n">compliance</span> - <span class="o">*</span> <span class="n">with</span> <span class="n">the</span> <span class="n">License</span><span class="p">.</span> <span class="n">You</span> <span class="n">may</span> <span class="n">obtain</span> <span class="n">a</span> <span class="n">copy</span> <span class="n">of</span> <span class="n">the</span> <span class="n">License</span> <span class="n">at</span> - <span class="o">*</span> - <span class="o">*</span> <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">www</span><span class="p">.</span><span class="n">apache</span><span class="p">.</span><span class="n">org</span><span class="o">/</span><span class="n">licenses</span><span class="o">/</span><span class="n">LICENSE</span><span class="o">-</span>2<span class="p">.</span>0 - <span class="o">*</span> - <span class="o">*</span> <span class="n">Unless</span> <span class="n">required</span> <span class="n">by</span> <span class="n">applicable</span> <span class="n">law</span> <span class="n">or</span> <span class="n">agreed</span> <span class="n">to</span> <span class="n">in</span> <span class="n">writing</span><span class="p">,</span> - <span class="o">*</span> <span class="n">software</span> <span class="n">distributed</span> <span class="n">under</span> <span class="n">the</span> <span class="n">License</span> <span class="n">is</span> <span class="n">distributed</span> <span class="n">on</span> <span class="n">an</span> - <span class="o">*</span> <span class="o">&</span><span class="n">quot</span><span class="p">;</span><span class="n">AS</span> <span class="n">IS</span><span class="o">&</span><span class="n">quot</span><span class="p">;</span> <span class="n">BASIS</span><span class="p">,</span> <span class="n">WITHOUT</span> <span class="n">WARRANTIES</span> <span class="n">OR</span> <span class="n">CONDITIONS</span> <span class="n">OF</span> <span class="n">ANY</span> - <span class="o">*</span> <span class="n">KIND</span><span class="p">,</span> <span class="n">either</span> <span class="n">express</span> <span class="n">or</span> <span class="n">implied</span><span class="p">.</span> <span class="n">See</span> <span class="n">the</span> <span class="n">License</span> <span class="k">for</span> <span class="n">the</span> - <span class="o">*</span> <span class="n">specific</span> <span class="n">language</span> <span class="n">governing</span> <span class="n">permissions</span> <span class="n">and</span> <span class="n">limitations</span> - <span class="o">*</span> <span class="n">under</span> <span class="n">the</span> <span class="n">License</span><span class="p">.</span> - <span class="o">*/</span> - -<span class="n">use</span> <span class="n">Thrift</span><span class="o">\</span><span class="n">Protocol</span><span class="o">\</span><span class="n">TBinaryProtocol</span><span class="p">;</span> -<span class="n">use</span> <span class="n">Thrift</span><span class="o">\</span><span class="n">Transport</span><span class="o">\</span><span class="n">TSocket</span><span class="p">;</span> -<span class="n">use</span> <span class="n">Thrift</span><span class="o">\</span><span class="n">Transport</span><span class="o">\</span><span class="n">THttpClient</span><span class="p">;</span> -<span class="n">use</span> <span class="n">Thrift</span><span class="o">\</span><span class="n">Transport</span><span class="o">\</span><span class="n">TBufferedTransport</span><span class="p">;</span> -<span class="n">use</span> <span class="n">Thrift</span><span class="o">\</span><span class="n">Exception</span><span class="o">\</span><span class="n">TException</span><span class="p">;</span> - -<span class="k">try</span> <span class="p">{</span> - <span class="k">if</span> <span class="p">(</span><span class="n">array_search</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;</span><span class="o">--</span><span class="n">http</span><span class="o">&</span>#39<span class="p">;,</span> $<span class="n">argv</span><span class="p">))</span> <span class="p">{</span> - $<span class="n">socket</span> <span class="p">=</span> <span class="n">new</span> <span class="n">THttpClient</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;</span><span class="n">localhost</span><span class="o">&</span>#39<span class="p">;,</span> 8080<span class="p">,</span> <span class="o">&</span>#39<span class="p">;</span><span class="o">/</span><span class="n">php</span><span class="o">/</span><span class="n">PhpServer</span><span class="p">.</span><span class="n">php</span><span class="o">&</span>#39<span class="p">;);</span> +<p><div class="codehilite"><pre><span class="cp"><?php</span></p> +<p><span class="k">namespace</span> <span class="nx">tutorial\php</span><span class="p">;</span></p> +<p><span class="nb">error_reporting</span><span class="p">(</span><span class="k">E_ALL</span><span class="p">);</span></p> +<p><span class="k">require_once</span> <span class="nx"><strong>DIR</strong></span><span class="o">.</span><span class="s1">'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'</span><span class="p">;</span></p> +<p><span class="k">use</span> <span class="nx">Thrift\ClassLoader\ThriftClassLoader</span><span class="p">;</span></p> +<p><span class="nv">$GEN_DIR</span> <span class="o">=</span> <span class="nb">realpath</span><span class="p">(</span><span class="nb">dirname</span><span class="p">(</span><span class="k"><strong>FILE</strong></span><span class="p">)</span><span class="o">.</span><span class="s1">'/..'</span><span class="p">)</span><span class="o">.</span><span class="s1">'/gen-php'</span><span class="p">;</span></p> +<p><span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ThriftClassLoader</span><span class="p">();</span> +<span class="nv">$loader</span><span class="o">-></span><span class="na">registerNamespace</span><span class="p">(</span><span class="s1">'Thrift'</span><span class="p">,</span> <span class="nx"><strong>DIR</strong></span> <span class="o">.</span> <span class="s1">'/../../lib/php/lib'</span><span class="p">);</span> +<span class="nv">$loader</span><span class="o">-></span><span class="na">registerDefinition</span><span class="p">(</span><span class="s1">'shared'</span><span class="p">,</span> <span class="nv">$GEN_DIR</span><span class="p">);</span> +<span class="nv">$loader</span><span class="o">-></span><span class="na">registerDefinition</span><span class="p">(</span><span class="s1">'tutorial'</span><span class="p">,</span> <span class="nv">$GEN_DIR</span><span class="p">);</span> +<span class="nv">$loader</span><span class="o">-></span><span class="na">register</span><span class="p">();</span></p> +<p><span class="cm">/<em></span> +<span class="cm"> * Licensed to the Apache Software Foundation (ASF) under one</span> +<span class="cm"> * or more contributor license agreements. See the NOTICE file</span> +<span class="cm"> * distributed with this work for additional information</span> +<span class="cm"> * regarding copyright ownership. The ASF licenses this file</span> +<span class="cm"> * to you under the Apache License, Version 2.0 (the</span> +<span class="cm"> * "License"); you may not use this file except in compliance</span> +<span class="cm"> * with the License. You may obtain a copy of the License at</span> +<span class="cm"> </em></span> +<span class="cm"> * http://www.apache.org/licenses/LICENSE-2.0</span> +<span class="cm"> <em></span> +<span class="cm"> * Unless required by applicable law or agreed to in writing,</span> +<span class="cm"> * software distributed under the License is distributed on an</span> +<span class="cm"> * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span> +<span class="cm"> * KIND, either express or implied. See the License for the</span> +<span class="cm"> * specific language governing permissions and limitations</span> +<span class="cm"> * under the License.</span> +<span class="cm"> </em>/</span></p> +<p><span class="k">use</span> <span class="nx">Thrift\Protocol\TBinaryProtocol</span><span class="p">;</span> +<span class="k">use</span> <span class="nx">Thrift\Transport\TSocket</span><span class="p">;</span> +<span class="k">use</span> <span class="nx">Thrift\Transport\THttpClient</span><span class="p">;</span> +<span class="k">use</span> <span class="nx">Thrift\Transport\TBufferedTransport</span><span class="p">;</span> +<span class="k">use</span> <span class="nx">Thrift\Exception\TException</span><span class="p">;</span></p> +<p><span class="k">try</span> <span class="p">{</span> + <span class="k">if</span> <span class="p">(</span><span class="nb">array_search</span><span class="p">(</span><span class="s1">'--http'</span><span class="p">,</span> <span class="nv">$argv</span><span class="p">))</span> <span class="p">{</span> + <span class="nv">$socket</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">THttpClient</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">,</span> <span class="mi">8080</span><span class="p">,</span> <span class="s1">'/php/PhpServer.php'</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> - $<span class="n">socket</span> <span class="p">=</span> <span class="n">new</span> <span class="n">TSocket</span><span class="p">(</span><span class="o">&</span>#39<span class="p">;</span><span class="n">localhost</span><span class="o">&</span>#39<span class="p">;,</span> 9090<span class="p">);</span> + <span class="nv">$socket</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">TSocket</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">,</span> <span class="mi">9090</span><span class="p">);</span> <span class="p">}</span> - $<span class="n">transport</span> <span class="p">=</span> <span class="n">new</span> <span class="n">TBufferedTransport</span><span class="p">(</span>$<span class="n">socket</span><span class="p">,</span> 1024<span class="p">,</span> 1024<span class="p">);</span> - $<span class="n">protocol</span> <span class="p">=</span> <span class="n">new</span> <span class="n">TBinaryProtocol</span><span class="p">(</span>$<span class="n">transport</span><span class="p">);</span> - $<span class="n">client</span> <span class="p">=</span> <span class="n">new</span> <span class="o">\</span><span class="n">tutorial</span><span class="o">\</span><span class="n">CalculatorClient</span><span class="p">(</span>$<span class="n">protocol</span><span class="p">);</span> - - $<span class="n">transport</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">open</span><span class="p">();</span> - - $<span class="n">client</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">ping</span><span class="p">();</span> - <span class="n">print</span> <span class="o">&</span><span class="n">quot</span><span class="p">;</span><span class="n">ping</span><span class="p">()</span><span class="o">\</span><span class="n">n</span><span class="o">&</span><span class="n">quot</span><span class="p">;;</span> - - $<span class="n">sum</span> <span class="p">=</span> $<span class="n">client</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">add</span><span class="p">(</span>1<span class="p">,</span>1<span class="p">);</span> - <span class="n">print</span> <span class="o">&</span><span class="n">quot</span><span class="p">;</span>1<span class="o">+</span>1<span class="p">=</span>$<span class="n">sum</span><span class="o">\</span><span class="n">n</span><span class="o">&</span><span class="n">quot</span><span class="p">;;</span> - - $<span class="n">work</span> <span class="p">=</span> <span class="n">new</span> <span class="o">\</span><span class="n">tutorial</span><span class="o">\</span><span class="n">Work</span><span class="p">();</span> - - $<span class="n">work</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">op</span> <span class="p">=</span> <span class="o">\</span><span class="n">tutorial</span><span class="o">\</span><span class="n">Operation</span><span class="p">::</span><span class="n">DIVIDE</span><span class="p">;</span> - $<span class="n">work</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">num1</span> <span class="p">=</span> 1<span class="p">;</span> - $<span class="n">work</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">num2</span> <span class="p">=</span> 0<span class="p">;</span> - - <span class="k">try</span> <span class="p">{</span> - $<span class="n">client</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">calculate</span><span class="p">(</span>1<span class="p">,</span> $<span class="n">work</span><span class="p">);</span> - <span class="n">print</span> <span class="o">&</span><span class="n">quot</span><span class="p">;</span><span class="n">Whoa</span>! <span class="n">We</span> <span class="n">can</span> <span class="n">divide</span> <span class="n">by</span> <span class="n">zero</span>?<span class="o">\</span><span class="n">n</span><span class="o">&</span><span class="n">quot</span><span class="p">;;</span> - <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="o">\</span><span class="n">tutorial</span><span class="o">\</span><span class="n">InvalidOperation</span> $<span class="n">io</span><span class="p">)</span> <span class="p">{</span> - <span class="n">print</span> <span class="o">&</span><span class="n">quot</span><span class="p">;</span><span class="n">InvalidOperation</span><span class="p">:</span> $<span class="n">io</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="nb">why</span><span class="o">\</span><span class="n">n</span><span class="o">&</span><span class="n">quot</span><span class="p">;;</span> - <span class="p">}</span> - - $<span class="n">work</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">op</span> <span class="p">=</span> <span class="o">\</span><span class="n">tutorial</span><span class="o">\</span><span class="n">Operation</span><span class="p">::</span><span class="n">SUBTRACT</span><span class="p">;</span> - $<span class="n">work</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">num1</span> <span class="p">=</span> 15<span class="p">;</span> - $<span class="n">work</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">num2</span> <span class="p">=</span> 10<span class="p">;</span> - $<span class="n">diff</span> <span class="p">=</span> $<span class="n">client</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">calculate</span><span class="p">(</span>1<span class="p">,</span> $<span class="n">work</span><span class="p">);</span> - <span class="n">print</span> <span class="o">&</span><span class="n">quot</span><span class="p">;</span>15<span class="o">-</span>10<span class="p">=</span>$<span class="n">diff</span><span class="o">\</span><span class="n">n</span><span class="o">&</span><span class="n">quot</span><span class="p">;;</span> - - $<span class="nb">log</span> <span class="p">=</span> $<span class="n">client</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">getStruct</span><span class="p">(</span>1<span class="p">);</span> - <span class="n">print</span> <span class="o">&</span><span class="n">quot</span><span class="p">;</span><span class="n">Log</span><span class="p">:</span> $<span class="nb">log</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">value</span><span class="o">\</span><span class="n">n</span><span class="o">&</span><span class="n">quot</span><span class="p">;;</span> - - $<span class="n">transport</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">close</span><span class="p">();</span> - -<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">TException</span> $<span class="n">tx</span><span class="p">)</span> <span class="p">{</span> - <span class="n">print</span> <span class="o">&</span>#39<span class="p">;</span><span class="n">TException</span><span class="p">:</span> <span class="o">&</span>#39<span class="p">;.</span>$<span class="n">tx</span><span class="o">-&</span><span class="n">gt</span><span class="p">;</span><span class="n">getMessage</span><span class="p">().</span><span class="o">&</span><span class="n">quot</span><span class="p">;</span><span class="o">\</span><span class="n">n</span><span class="o">&</span><span class="n">quot</span><span class="p">;;</span> -<span class="p">}</span> - -?<span class="o">&</span><span class="n">gt</span><span class="p">;</span> -</pre></div> - - + <span class="nv">$transport</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">TBufferedTransport</span><span class="p">(</span><span class="nv">$socket</span><span class="p">,</span> <span class="mi">1024</span><span class="p">,</span> <span class="mi">1024</span><span class="p">);</span> + <span class="nv">$protocol</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">TBinaryProtocol</span><span class="p">(</span><span class="nv">$transport</span><span class="p">);</span> + <span class="nv">$client</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\tutorial\CalculatorClient</span><span class="p">(</span><span class="nv">$protocol</span><span class="p">);</span></p> +<p><span class="nv">$transport</span><span class="o">-></span><span class="na">open</span><span class="p">();</span></p> +<p><span class="nv">$client</span><span class="o">-></span><span class="na">ping</span><span class="p">();</span> + <span class="k">print</span> <span class="s2">"ping()</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span></p> +<p><span class="nv">$sum</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span> + <span class="k">print</span> <span class="s2">"1+1=</span><span class="si">$sum</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span></p> +<p><span class="nv">$work</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\tutorial\Work</span><span class="p">();</span></p> +<p><span class="nv">$work</span><span class="o">-></span><span class="na">op</span> <span class="o">=</span> <span class="nx">\tutorial\Operation</span><span class="o">::</span><span class="na">DIVIDE</span><span class="p">;</span> + <span class="nv">$work</span><span class="o">-></span><span class="na">num1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> + <span class="nv">$work</span><span class="o">-></span><span class="na">num2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span></p> +<p><span class="k">try</span> <span class="p">{</span> + <span class="nv">$client</span><span class="o">-></span><span class="na">calculate</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nv">$work</span><span class="p">);</span> + <span class="k">print</span> <span class="s2">"Whoa! We can divide by zero?</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span> + <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">\tutorial\InvalidOperation</span> <span class="nv">$io</span><span class="p">)</span> <span class="p">{</span> + <span class="k">print</span> <span class="s2">"InvalidOperation: </span><span class="si">$io->why</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span> + <span class="p">}</span></p> +<p><span class="nv">$work</span><span class="o">-></span><span class="na">op</span> <span class="o">=</span> <span class="nx">\tutorial\Operation</span><span class="o">::</span><span class="na">SUBTRACT</span><span class="p">;</span> + <span class="nv">$work</span><span class="o">-></span><span class="na">num1</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> + <span class="nv">$work</span><span class="o">-></span><span class="na">num2</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span> + <span class="nv">$diff</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">calculate</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nv">$work</span><span class="p">);</span> + <span class="k">print</span> <span class="s2">"15-10=</span><span class="si">$diff</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span></p> +<p><span class="nv">$log</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getStruct</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> + <span class="k">print</span> <span class="s2">"Log: </span><span class="si">$log->value</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span></p> +<p><span class="nv">$transport</span><span class="o">-></span><span class="na">close</span><span class="p">();</span></p> +<p><span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">TException</span> <span class="nv">$tx</span><span class="p">)</span> <span class="p">{</span> + <span class="k">print</span> <span class="s1">'TException: '</span><span class="o">.</span><span class="nv">$tx</span><span class="o">-></span><span class="na">getMessage</span><span class="p">()</span><span class="o">.</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span> +<span class="p">}</span></p> +<p><span class="cp">?></span><span class="x"></span> +</pre></div></p> <h3 id="server">Server</h3> -<div class="codehilite"><pre><span class="x"> :::php</span> -<span class="x">&lt;?php</span> - -<span class="x">namespace tutorial\php;</span> - -<span class="x">error_reporting(E_ALL);</span> - -<span class="x">require_once __DIR__.&#39;/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php&#39;;</span> - -<span class="x">use Thrift\ClassLoader\ThriftClassLoader;</span> - -<span class="x">$GEN_DIR = realpath(dirname(__FILE__).&#39;/..&#39;).&#39;/gen-php&#39;;</span> - -<span class="x">$loader = new ThriftClassLoader();</span> -<span class="x">$loader-&gt;registerNamespace(&#39;Thrift&#39;, __DIR__ . &#39;/../../lib/php/lib&#39;);</span> -<span class="x">$loader-&gt;registerDefinition(&#39;shared&#39;, $GEN_DIR);</span> -<span class="x">$loader-&gt;registerDefinition(&#39;tutorial&#39;, $GEN_DIR);</span> -<span class="x">$loader-&gt;register();</span> - -<span class="x">/*</span> -<span class="x"> * Licensed to the Apache Software Foundation (ASF) under one</span> -<span class="x"> * or more contributor license agreements. See the NOTICE file</span> -<span class="x"> * distributed with this work for additional information</span> -<span class="x"> * regarding copyright ownership. The ASF licenses this file</span> -<span class="x"> * to you under the Apache License, Version 2.0 (the</span> -<span class="x"> * &quot;License&quot;); you may not use this file except in compliance</span> -<span class="x"> * with the License. You may obtain a copy of the License at</span> -<span class="x"> *</span> -<span class="x"> * http://www.apache.org/licenses/LICENSE-2.0</span> -<span class="x"> *</span> -<span class="x"> * Unless required by applicable law or agreed to in writing,</span> -<span class="x"> * software distributed under the License is distributed on an</span> -<span class="x"> * &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span> -<span class="x"> * KIND, either express or implied. See the License for the</span> -<span class="x"> * specific language governing permissions and limitations</span> -<span class="x"> * under the License.</span> -<span class="x"> */</span> - -<span class="x">/*</span> -<span class="x"> * This is not a stand-alone server. It should be run as a normal</span> -<span class="x"> * php web script (like through Apache&#39;s mod_php) or as a cgi script</span> -<span class="x"> * (like with the included runserver.py). You can connect to it with</span> -<span class="x"> * THttpClient in any language that supports it. The PHP tutorial client</span> -<span class="x"> * will work if you pass it the argument &quot;--http&quot;.</span> -<span class="x"> */</span> - -<span class="x">if (php_sapi_name() == &#39;cli&#39;) {</span> -<span class="x"> ini_set(&quot;display_errors&quot;, &quot;stderr&quot;);</span> -<span class="x">}</span> - -<span class="x">use Thrift\Protocol\TBinaryProtocol;</span> -<span class="x">use Thrift\Transport\TPhpStream;</span> -<span class="x">use Thrift\Transport\TBufferedTransport;</span> - -<span class="x">class CalculatorHandler implements \tutorial\CalculatorIf {</span> -<span class="x"> protected $log = array();</span> - -<span class="x"> public function ping() {</span> -<span class="x"> error_log(&quot;ping()&quot;);</span> -<span class="x"> }</span> - -<span class="x"> public function add($num1, $num2) {</span> -<span class="x"> error_log(&quot;add({$num1}, {$num2})&quot;);</span> -<span class="x"> return $num1 + $num2;</span> -<span class="x"> }</span> - -<span class="x"> public function calculate($logid, \tutorial\Work $w) {</span> -<span class="x"> error_log(&quot;calculate({$logid}, </span><span class="cp">{{</span><span class="err">$</span><span class="nv">w-</span><span class="err">&</span><span class="nv">gt</span><span class="err">;</span><span class="nv">op</span><span class="o">},</span> <span class="o">{</span><span class="err">$</span><span class="nv">w-</span><span class="err">&</span><span class="nv">gt</span><span class="err">;</span><span class="nv">num1</span><span class="o">},</span> <span class="o">{</span><span class="err">$</span><span class="nv">w-</span><span class="err">&</span><span class="nv">gt</span><span class="err">;</span><span class="nv">num2</span><span class="cp">}}</span><span class="x">)&quot;);</span> -<span class="x"> switch ($w-&gt;op) {</span> -<span class="x"> case \tutorial\Operation::ADD:</span> -<span class="x"> $val = $w-&gt;num1 + $w-&gt;num2;</span> -<span class="x"> break;</span> -<span class="x"> case \tutorial\Operation::SUBTRACT:</span> -<span class="x"> $val = $w-&gt;num1 - $w-&gt;num2;</span> -<span class="x"> break;</span> -<span class="x"> case \tutorial\Operation::MULTIPLY:</span> -<span class="x"> $val = $w-&gt;num1 * $w-&gt;num2;</span> -<span class="x"> break;</span> -<span class="x"> case \tutorial\Operation::DIVIDE:</span> -<span class="x"> if ($w-&gt;num2 == 0) {</span> -<span class="x"> $io = new \tutorial\InvalidOperation();</span> -<span class="x"> $io-&gt;what = $w-&gt;op;</span> -<span class="x"> $io-&gt;why = &quot;Cannot divide by 0&quot;;</span> -<span class="x"> throw $io;</span> -<span class="x"> }</span> -<span class="x"> $val = $w-&gt;num1 / $w-&gt;num2;</span> -<span class="x"> break;</span> -<span class="x"> default:</span> -<span class="x"> $io = new \tutorial\InvalidOperation();</span> -<span class="x"> $io-&gt;what = $w-&gt;op;</span> -<span class="x"> $io-&gt;why = &quot;Invalid Operation&quot;;</span> -<span class="x"> throw $io;</span> -<span class="x"> }</span> - -<span class="x"> $log = new \shared\SharedStruct();</span> -<span class="x"> $log-&gt;key = $logid;</span> -<span class="x"> $log-&gt;value = (string)$val;</span> -<span class="x"> $this-&gt;log[$logid] = $log;</span> - -<span class="x"> return $val;</span> -<span class="x"> }</span> - -<span class="x"> public function getStruct($key) {</span> -<span class="x"> error_log(&quot;getStruct({$key})&quot;);</span> -<span class="x"> // This actually doesn&#39;t work because the PHP interpreter is</span> -<span class="x"> // restarted for every request.</span> -<span class="x"> //return $this-&gt;log[$key];</span> -<span class="x"> return new \shared\SharedStruct(array(&quot;key&quot; =&gt; $key, &quot;value&quot; =&gt; &quot;PHP is stateless!&quot;));</span> -<span class="x"> }</span> - -<span class="x"> public function zip() {</span> -<span class="x"> error_log(&quot;zip()&quot;);</span> -<span class="x"> }</span> - -<span class="x">};</span> - -<span class="x">header(&#39;Content-Type&#39;, &#39;application/x-thrift&#39;);</span> -<span class="x">if (php_sapi_name() == &#39;cli&#39;) {</span> -<span class="x"> echo &quot;\r\n&quot;;</span> -<span class="x">}</span> - -<span class="x">$handler = new CalculatorHandler();</span> -<span class="x">$processor = new \tutorial\CalculatorProcessor($handler);</span> - -<span class="x">$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));</span> -<span class="x">$protocol = new TBinaryProtocol($transport, true, true);</span> +<p><div class="codehilite"><pre><span class="cp"><?php</span></p> +<p><span class="k">namespace</span> <span class="nx">tutorial\php</span><span class="p">;</span></p> +<p><span class="nb">error_reporting</span><span class="p">(</span><span class="k">E_ALL</span><span class="p">);</span></p> +<p><span class="k">require_once</span> <span class="nx"><strong>DIR</strong></span><span class="o">.</span><span class="s1">'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'</span><span class="p">;</span></p> +<p><span class="k">use</span> <span class="nx">Thrift\ClassLoader\ThriftClassLoader</span><span class="p">;</span></p> +<p><span class="nv">$GEN_DIR</span> <span class="o">=</span> <span class="nb">realpath</span><span class="p">(</span><span class="nb">dirname</span><span class="p">(</span><span class="k"><strong>FILE</strong></span><span class="p">)</span><span class="o">.</span><span class="s1">'/..'</span><span class="p">)</span><span class="o">.</span><span class="s1">'/gen-php'</span><span class="p">;</span></p> +<p><span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ThriftClassLoader</span><span class="p">();</span> +<span class="nv">$loader</span><span class="o">-></span><span class="na">registerNamespace</span><span class="p">(</span><span class="s1">'Thrift'</span><span class="p">,</span> <span class="nx"><strong>DIR</strong></span> <span class="o">.</span> <span class="s1">'/../../lib/php/lib'</span><span class="p">);</span> +<span class="nv">$loader</span><span class="o">-></span><span class="na">registerDefinition</span><span class="p">(</span><span class="s1">'shared'</span><span class="p">,</span> <span class="nv">$GEN_DIR</span><span class="p">);</span> +<span class="nv">$loader</span><span class="o">-></span><span class="na">registerDefinition</span><span class="p">(</span><span class="s1">'tutorial'</span><span class="p">,</span> <span class="nv">$GEN_DIR</span><span class="p">);</span> +<span class="nv">$loader</span><span class="o">-></span><span class="na">register</span><span class="p">();</span></p> +<p><span class="cm">/<em></span> +<span class="cm"> * Licensed to the Apache Software Foundation (ASF) under one</span> +<span class="cm"> * or more contributor license agreements. See the NOTICE file</span> +<span class="cm"> * distributed with this work for additional information</span> +<span class="cm"> * regarding copyright ownership. The ASF licenses this file</span> +<span class="cm"> * to you under the Apache License, Version 2.0 (the</span> +<span class="cm"> * "License"); you may not use this file except in compliance</span> +<span class="cm"> * with the License. You may obtain a copy of the License at</span> +<span class="cm"> </em></span> +<span class="cm"> * http://www.apache.org/licenses/LICENSE-2.0</span> +<span class="cm"> <em></span> +<span class="cm"> * Unless required by applicable law or agreed to in writing,</span> +<span class="cm"> * software distributed under the License is distributed on an</span> +<span class="cm"> * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY</span> +<span class="cm"> * KIND, either express or implied. See the License for the</span> +<span class="cm"> * specific language governing permissions and limitations</span> +<span class="cm"> * under the License.</span> +<span class="cm"> </em>/</span></p> +<p><span class="cm">/<em></span> +<span class="cm"> * This is not a stand-alone server. It should be run as a normal</span> +<span class="cm"> * php web script (like through Apache's mod_php) or as a cgi script</span> +<span class="cm"> * (like with the included runserver.py). You can connect to it with</span> +<span class="cm"> * THttpClient in any language that supports it. The PHP tutorial client</span> +<span class="cm"> * will work if you pass it the argument "--http".</span> +<span class="cm"> </em>/</span></p> +<p><span class="k">if</span> <span class="p">(</span><span class="nb">php_sapi_name</span><span class="p">()</span> <span class="o">==</span> <span class="s1">'cli'</span><span class="p">)</span> <span class="p">{</span> + <span class="nb">ini_set</span><span class="p">(</span><span class="s2">"display_errors"</span><span class="p">,</span> <span class="s2">"stderr"</span><span class="p">);</span> +<span class="p">}</span></p> +<p><span class="k">use</span> <span class="nx">Thrift\Protocol\TBinaryProtocol</span><span class="p">;</span> +<span class="k">use</span> <span class="nx">Thrift\Transport\TPhpStream</span><span class="p">;</span> +<span class="k">use</span> <span class="nx">Thrift\Transport\TBufferedTransport</span><span class="p">;</span></p> +<p><span class="k">class</span> <span class="nc">CalculatorHandler</span> <span class="k">implements</span> <span class="nx">\tutorial\CalculatorIf</span> <span class="p">{</span> + <span class="k">protected</span> <span class="nv">$log</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span></p> +<p><span class="k">public</span> <span class="k">function</span> <span class="nf">ping</span><span class="p">()</span> <span class="p">{</span> + <span class="nb">error_log</span><span class="p">(</span><span class="s2">"ping()"</span><span class="p">);</span> + <span class="p">}</span></p> +<p><span class="k">public</span> <span class="k">function</span> <span class="nf">add</span><span class="p">(</span><span class="nv">$num1</span><span class="p">,</span> <span class="nv">$num2</span><span class="p">)</span> <span class="p">{</span> + <span class="nb">error_log</span><span class="p">(</span><span class="s2">"add(</span><span class="si">{</span><span class="nv">$num1</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="nv">$num2</span><span class="si">}</span><span class="s2">)"</span><span class="p">);</span> + <span class="k">return</span> <span class="nv">$num1</span> <span class="o">+</span> <span class="nv">$num2</span><span class="p">;</span> + <span class="p">}</span></p> +<p><span class="k">public</span> <span class="k">function</span> <span class="nf">calculate</span><span class="p">(</span><span class="nv">$logid</span><span class="p">,</span> <span class="nx">\tutorial\Work</span> <span class="nv">$w</span><span class="p">)</span> <span class="p">{</span> + <span class="nb">error_log</span><span class="p">(</span><span class="s2">"calculate(</span><span class="si">{</span><span class="nv">$logid</span><span class="si">}</span><span class="s2">, {{$w->op}, </span><span class="si">{</span><span class="nv">$w</span><span class="o">-></span><span class="na">num1</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="nv">$w</span><span class="o">-></span><span class="na">num2</span><span class="si">}</span><span class="s2">})"</span><span class="p">);</span> + <span class="k">switch</span> <span class="p">(</span><span class="nv">$w</span><span class="o">-></span><span class="na">op</span><span class="p">)</span> <span class="p">{</span> + <span class="k">case</span> <span class="nx">\tutorial\Operation</span><span class="o">::</span><span class="na">ADD</span><span class="o">:</span> + <span class="nv">$val</span> <span class="o">=</span> <span class="nv">$w</span><span class="o">-></span><span class="na">num1</span> <span class="o">+</span> <span class="nv">$w</span><span class="o">-></span><span class="na">num2</span><span class="p">;</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">case</span> <span class="nx">\tutorial\Operation</span><span class="o">::</span><span class="na">SUBTRACT</span><span class="o">:</span> + <span class="nv">$val</span> <span class="o">=</span> <span class="nv">$w</span><span class="o">-></span><span class="na">num1</span> <span class="o">-</span> <span class="nv">$w</span><span class="o">-></span><span class="na">num2</span><span class="p">;</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">case</span> <span class="nx">\tutorial\Operation</span><span class="o">::</span><span class="na">MULTIPLY</span><span class="o">:</span> + <span class="nv">$val</span> <span class="o">=</span> <span class="nv">$w</span><span class="o">-></span><span class="na">num1</span> <span class="o">*</span> <span class="nv">$w</span><span class="o">-></span><span class="na">num2</span><span class="p">;</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">case</span> <span class="nx">\tutorial\Operation</span><span class="o">::</span><span class="na">DIVIDE</span><span class="o">:</span> + <span class="k">if</span> <span class="p">(</span><span class="nv">$w</span><span class="o">-></span><span class="na">num2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> + <span class="nv">$io</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\tutorial\InvalidOperation</span><span class="p">();</span> + <span class="nv">$io</span><span class="o">-></span><span class="na">what</span> <span class="o">=</span> <span class="nv">$w</span><span class="o">-></span><span class="na">op</span><span class="p">;</span> + <span class="nv">$io</span><span class="o">-></span><span class="na">why</span> <span class="o">=</span> <span class="s2">"Cannot divide by 0"</span><span class="p">;</span> + <span class="k">throw</span> <span class="nv">$io</span><span class="p">;</span> + <span class="p">}</span> + <span class="nv">$val</span> <span class="o">=</span> <span class="nv">$w</span><span class="o">-></span><span class="na">num1</span> <span class="o">/</span> <span class="nv">$w</span><span class="o">-></span><span class="na">num2</span><span class="p">;</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">default</span><span class="o">:</span> + <span class="nv">$io</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\tutorial\InvalidOperation</span><span class="p">();</span> + <span class="nv">$io</span><span class="o">-></span><span class="na">what</span> <span class="o">=</span> <span class="nv">$w</span><span class="o">-></span><span class="na">op</span><span class="p">;</span> + <span class="nv">$io</span><span class="o">-></span><span class="na">why</span> <span class="o">=</span> <span class="s2">"Invalid Operation"</span><span class="p">;</span> + <span class="k">throw</span> <span class="nv">$io</span><span class="p">;</span> + <span class="p">}</span></p> +<div class="codehilite"><pre><span class="nt"><span</span> <span class="na">class=</span><span class="s">"nv"</span><span class="nt">></span><span class="p">$</span><span class="nv">log</span><span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>new<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"nx"</span><span class="nt">></span>\shared\SharedStruct<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>();<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"nv"</span><span class="nt">></span><span class="p">$</span><span class="nv">log</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>-<span class="ni">&gt;</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"na"</span><span class="nt">></span>key<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"nv"</span><span class="nt">></span><span class="p">$</span><span class="nv">logid</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><spa n class="nt">></span>;<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"nv"</span><span class="nt">></span><span class="p">$</span><span class="nv">log</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>-<span class="ni">&gt;</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"na"</span><span class="nt">></span>value<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>(<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"nx"</span><span class="nt">></span>string<span class="nt">< ;/span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>)<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"nv"</span><span class="nt">></span><span class="p">$</span><span class="nv">val</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"nv"</span><span class="nt">></span><span class="p">$</span><span class="nv">this</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>-<span class="ni">&gt;</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"na"</span><span class="nt">></span>log<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>[<span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"nv"</span><span class="nt">></span><span class="p">$</span><span class="nv">logid</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>]<span class="nt"></sp an></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"o"</span><span class="nt">></span>=<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"nv"</span><span class="nt">></span><span class="p">$</span><span class="nv">log</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> -<span class="x">$transport-&gt;open();</span> -<span class="x">$processor-&gt;process($protocol, $protocol);</span> -<span class="x">$transport-&gt;close();</span> +<span class="nt"><span</span> <span class="na">class=</span><span class="s">"k"</span><span class="nt">></span>return<span class="nt"></span></span> <span class="nt"><span</span> <span class="na">class=</span><span class="s">"nv"</span><span class="nt">></span><span class="p">$</span><span class="nv">val</span><span class="nt"></span><span</span> <span class="na">class=</span><span class="s">"p"</span><span class="nt">></span>;<span class="nt"></span></span> </pre></div> +<p><span class="p">}</span></p> +<p><span class="k">public</span> <span class="k">function</span> <span class="nf">getStruct</span><span class="p">(</span><span class="nv">$key</span><span class="p">)</span> <span class="p">{</span> + <span class="nb">error_log</span><span class="p">(</span><span class="s2">"getStruct(</span><span class="si">{</span><span class="nv">$key</span><span class="si">}</span><span class="s2">)"</span><span class="p">);</span> + <span class="c1">// This actually doesn't work because the PHP interpreter is</span> + <span class="c1">// restarted for every request.</span> + <span class="c1">//return $this->log[$key];</span> + <span class="k">return</span> <span class="k">new</span> <span class="nx">\shared\SharedStruct</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s2">"key"</span> <span class="o">=></span> <span class="nv">$key</span><span class="p">,</span> <span class="s2">"value"</span> <span class="o">=></span> <span class="s2">"PHP is stateless!"</span><span class="p">));</span> + <span class="p">}</span></p> +<p><span class="k">public</span> <span class="k">function</span> <span class="nf">zip</span><span class="p">()</span> <span class="p">{</span> + <span class="nb">error_log</span><span class="p">(</span><span class="s2">"zip()"</span><span class="p">);</span> + <span class="p">}</span></p> +<p><span class="p">};</span></p> +<p><span class="nx">header</span><span class="p">(</span><span class="s1">'Content-Type'</span><span class="p">,</span> <span class="s1">'application/x-thrift'</span><span class="p">);</span> +<span class="k">if</span> <span class="p">(</span><span class="nb">php_sapi_name</span><span class="p">()</span> <span class="o">==</span> <span class="s1">'cli'</span><span class="p">)</span> <span class="p">{</span> + <span class="k">echo</span> <span class="s2">"</span><span class="se">\r\n</span><span class="s2">"</span><span class="p">;</span> +<span class="p">}</span></p> +<p><span class="nv">$handler</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">CalculatorHandler</span><span class="p">();</span> +<span class="nv">$processor</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\tutorial\CalculatorProcessor</span><span class="p">(</span><span class="nv">$handler</span><span class="p">);</span></p> +<p><span class="nv">$transport</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">TBufferedTransport</span><span class="p">(</span><span class="k">new</span> <span class="nx">TPhpStream</span><span class="p">(</span><span class="nx">TPhpStream</span><span class="o">::</span><span class="na">MODE_R</span> <span class="o">|</span> <span class="nx">TPhpStream</span><span class="o">::</span><span class="na">MODE_W</span><span class="p">));</span> +<span class="nv">$protocol</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">TBinaryProtocol</span><span class="p">(</span><span class="nv">$transport</span><span class="p">,</span> <span class="k">true</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span></p> +<p><span class="nv">$transport</span><span class="o">-></span><span class="na">open</span><span class="p">();</span> +<span class="nv">$processor</span><span class="o">-></span><span class="na">process</span><span class="p">(</span><span class="nv">$protocol</span><span class="p">,</span> <span class="nv">$protocol</span><span class="p">);</span> +<span class="nv">$transport</span><span class="o">-></span><span class="na">close</span><span class="p">();</span> +</pre></div></p> <h2 id="additional-information">Additional Information</h2> </div>