PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/php/mysqlnd-uh.quickstart.how-it-works.html

https://bitbucket.org/stillzhl/manuals
HTML | 106 lines | 103 code | 3 blank | 0 comment | 0 complexity | 176fb09a8308eafe299581b2a4437b02 MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5. <title>How it works</title>
  6. </head>
  7. <body><div class="manualnavbar" style="text-align: center;">
  8. <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-uh.quickstart.configuration.html">Setup</a></div>
  9. <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-uh.quickstart.proxy-installation.html">Installing a proxy</a></div>
  10. <div class="up"><a href="mysqlnd-uh.quickstart.html">Quickstart and Examples</a></div>
  11. <div class="home"><a href="index.html">PHP Manual</a></div>
  12. </div><hr /><div id="mysqlnd-uh.quickstart.how-it-works" class="section">
  13. <h2 class="title">How it works</h2>
  14. <p class="para">
  15. This describes the background and inner workings of the mysqlnd_uh
  16. extension.
  17. </p>
  18. <p class="para">
  19. Two classes are provided by the extension: <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a>
  20. and <a href="class.mysqlnduhpreparedstatement.html" class="classname">MysqlndUhPreparedStatement</a>. <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a> lets
  21. you access almost all methods of the <em>mysqlnd</em>
  22. internal <em>connection</em> class. The latter exposes some selected
  23. methods of the <em>mysqlnd</em> internal <em>statement</em> class.
  24. For example, <span class="methodname"><a href="mysqlnduhconnection.connect.html" class="methodname">MysqlndUhConnection::connect()</a></span> maps to
  25. the <em>mysqlnd</em> library C function
  26. <em>mysqlnd_conn__connect</em>.
  27. </p>
  28. <p class="para">
  29. As a mysqlnd plugin, the PECL/mysqlnd_uh extension replaces <em>mysqlnd</em>
  30. library C functions with its own functions. Whenever a
  31. PHP MySQL extension compiled to use <em>mysqlnd</em> calls
  32. a mysqlnd function, the functions installed by the plugin are executed
  33. instead of the original <em>mysqlnd</em> ones. For example,
  34. <span class="function"><a href="function.mysqli-connect.html" class="function">mysqli_connect()</a></span> invokes <em>mysqlnd_conn__connect</em>,
  35. so the connect function installed by PECL/mysqlnd_uh will be called.
  36. The functions installed by PECL/mysqlnd_uh are the methods of the built-in classes.
  37. </p>
  38. <p class="para">
  39. The built-in PHP classes and their methods do nothing but call their
  40. <em>mysqlnd</em> C library counterparts, to behave exactly
  41. like the original <em>mysqlnd</em> function they replace.
  42. The code below illustrates in pseudo-code what the extension does.
  43. </p>
  44. <p class="para">
  45. <div class="example" id="example-1885">
  46. <p><strong>Example #1 Pseudo-code: what a built-in class does</strong></p>
  47. <div class="example-contents">
  48. <div class="cdata"><pre>
  49. class MysqlndUhConnection {
  50. public function connect(($conn, $host, $user, $passwd, $db, $port, $socket, $mysql_flags) {
  51. MYSQLND* c_mysqlnd_connection = convert_from_php_to_c($conn);
  52. ...
  53. return call_c_function(mysqlnd_conn__connect(c_mysqlnd_connection, ...));
  54. }
  55. }
  56. </pre></div>
  57. </div>
  58. </div>
  59. </p>
  60. <p class="para">
  61. The build-in classes behave like a transparent proxy. It is possible for
  62. you to replace the proxy with your own. This is done by subclassing
  63. <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a> or
  64. <a href="class.mysqlnduhpreparedstatement.html" class="classname">MysqlndUhPreparedStatement</a> to extend the functionality
  65. of the proxy, followed by registering a new proxy object.
  66. Proxy objects are installed by
  67. <span class="function"><a href="function.mysqlnd-uh-set-connection-proxy.html" class="function">mysqlnd_uh_set_connection_proxy()</a></span>
  68. and
  69. <span class="function"><a href="function.mysqlnd-uh-set-statement-proxy.html" class="function">mysqlnd_uh_set_statement_proxy()</a></span>.
  70. </p>
  71. <p class="para">
  72. <div class="example" id="example-1886">
  73. <p><strong>Example #2 Installing a proxy</strong></p>
  74. <div class="example-contents">
  75. <div class="phpcode"><code><span style="color: #000000">
  76. <span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">proxy&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">MysqlndUhConnection&nbsp;</span><span style="color: #007700">{<br />&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$host</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$passwd</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$port</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$mysql_flags</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s(%s)\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">func_get_args</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$host</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$passwd</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$port</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$mysql_flags</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s&nbsp;returns&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$ret</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$ret</span><span style="color: #007700">;<br />&nbsp;}<br />}<br /></span><span style="color: #0000BB">mysqlnd_uh_set_connection_proxy</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">proxy</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">$mysqli&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"root"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
  77. </span>
  78. </code></div>
  79. </div>
  80. <div class="example-contents"><p>以上例程会输出</p></div>
  81. <div class="example-contents screen">
  82. <div class="cdata"><pre>
  83. proxy::connect(array (
  84. 0 =&gt; NULL,
  85. 1 =&gt; &#039;localhost&#039;,
  86. 2 =&gt; &#039;root&#039;,
  87. 3 =&gt; &#039;&#039;,
  88. 4 =&gt; &#039;test&#039;,
  89. 5 =&gt; 3306,
  90. 6 =&gt; NULL,
  91. 7 =&gt; 131072,
  92. ))
  93. proxy::connect returns true
  94. </pre></div>
  95. </div>
  96. </div>
  97. </p>
  98. </div><hr /><div class="manualnavbar" style="text-align: center;">
  99. <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-uh.quickstart.configuration.html">Setup</a></div>
  100. <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-uh.quickstart.proxy-installation.html">Installing a proxy</a></div>
  101. <div class="up"><a href="mysqlnd-uh.quickstart.html">Quickstart and Examples</a></div>
  102. <div class="home"><a href="index.html">PHP Manual</a></div>
  103. </div></body></html>