/php/mysqlnd-uh.quickstart.how-it-works.html
HTML | 106 lines | 103 code | 3 blank | 0 comment | 0 complexity | 176fb09a8308eafe299581b2a4437b02 MD5 | raw file
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <title>How it works</title>
- </head>
- <body><div class="manualnavbar" style="text-align: center;">
- <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-uh.quickstart.configuration.html">Setup</a></div>
- <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-uh.quickstart.proxy-installation.html">Installing a proxy</a></div>
- <div class="up"><a href="mysqlnd-uh.quickstart.html">Quickstart and Examples</a></div>
- <div class="home"><a href="index.html">PHP Manual</a></div>
- </div><hr /><div id="mysqlnd-uh.quickstart.how-it-works" class="section">
- <h2 class="title">How it works</h2>
- <p class="para">
- This describes the background and inner workings of the mysqlnd_uh
- extension.
- </p>
- <p class="para">
- Two classes are provided by the extension: <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a>
- and <a href="class.mysqlnduhpreparedstatement.html" class="classname">MysqlndUhPreparedStatement</a>. <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a> lets
- you access almost all methods of the <em>mysqlnd</em>
- internal <em>connection</em> class. The latter exposes some selected
- methods of the <em>mysqlnd</em> internal <em>statement</em> class.
- For example, <span class="methodname"><a href="mysqlnduhconnection.connect.html" class="methodname">MysqlndUhConnection::connect()</a></span> maps to
- the <em>mysqlnd</em> library C function
- <em>mysqlnd_conn__connect</em>.
- </p>
- <p class="para">
- As a mysqlnd plugin, the PECL/mysqlnd_uh extension replaces <em>mysqlnd</em>
- library C functions with its own functions. Whenever a
- PHP MySQL extension compiled to use <em>mysqlnd</em> calls
- a mysqlnd function, the functions installed by the plugin are executed
- instead of the original <em>mysqlnd</em> ones. For example,
- <span class="function"><a href="function.mysqli-connect.html" class="function">mysqli_connect()</a></span> invokes <em>mysqlnd_conn__connect</em>,
- so the connect function installed by PECL/mysqlnd_uh will be called.
- The functions installed by PECL/mysqlnd_uh are the methods of the built-in classes.
- </p>
- <p class="para">
- The built-in PHP classes and their methods do nothing but call their
- <em>mysqlnd</em> C library counterparts, to behave exactly
- like the original <em>mysqlnd</em> function they replace.
- The code below illustrates in pseudo-code what the extension does.
- </p>
- <p class="para">
- <div class="example" id="example-1885">
- <p><strong>Example #1 Pseudo-code: what a built-in class does</strong></p>
- <div class="example-contents">
- <div class="cdata"><pre>
- class MysqlndUhConnection {
- public function connect(($conn, $host, $user, $passwd, $db, $port, $socket, $mysql_flags) {
- MYSQLND* c_mysqlnd_connection = convert_from_php_to_c($conn);
- ...
- return call_c_function(mysqlnd_conn__connect(c_mysqlnd_connection, ...));
- }
- }
- </pre></div>
- </div>
- </div>
- </p>
- <p class="para">
- The build-in classes behave like a transparent proxy. It is possible for
- you to replace the proxy with your own. This is done by subclassing
- <a href="class.mysqlnduhconnection.html" class="classname">MysqlndUhConnection</a> or
- <a href="class.mysqlnduhpreparedstatement.html" class="classname">MysqlndUhPreparedStatement</a> to extend the functionality
- of the proxy, followed by registering a new proxy object.
- Proxy objects are installed by
- <span class="function"><a href="function.mysqlnd-uh-set-connection-proxy.html" class="function">mysqlnd_uh_set_connection_proxy()</a></span>
- and
- <span class="function"><a href="function.mysqlnd-uh-set-statement-proxy.html" class="function">mysqlnd_uh_set_statement_proxy()</a></span>.
- </p>
- <p class="para">
- <div class="example" id="example-1886">
- <p><strong>Example #2 Installing a proxy</strong></p>
- <div class="example-contents">
- <div class="phpcode"><code><span style="color: #000000">
- <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">proxy </span><span style="color: #007700">extends </span><span style="color: #0000BB">MysqlndUhConnection </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">, </span><span style="color: #0000BB">$host</span><span style="color: #007700">, </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$passwd</span><span style="color: #007700">, </span><span style="color: #0000BB">$db</span><span style="color: #007700">, </span><span style="color: #0000BB">$port</span><span style="color: #007700">, </span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">$mysql_flags</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s(%s)\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </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">(), </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /> </span><span style="color: #0000BB">$ret </span><span style="color: #007700">= </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">, </span><span style="color: #0000BB">$host</span><span style="color: #007700">, </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$passwd</span><span style="color: #007700">, </span><span style="color: #0000BB">$db</span><span style="color: #007700">, </span><span style="color: #0000BB">$port</span><span style="color: #007700">, </span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">$mysql_flags</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s returns %s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$ret</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /> return </span><span style="color: #0000BB">$ret</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">mysqlnd_uh_set_connection_proxy</span><span style="color: #007700">(new </span><span style="color: #0000BB">proxy</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"root"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
- </span>
- </code></div>
- </div>
- <div class="example-contents"><p>以上例程会输出:</p></div>
- <div class="example-contents screen">
- <div class="cdata"><pre>
- proxy::connect(array (
- 0 => NULL,
- 1 => 'localhost',
- 2 => 'root',
- 3 => '',
- 4 => 'test',
- 5 => 3306,
- 6 => NULL,
- 7 => 131072,
- ))
- proxy::connect returns true
- </pre></div>
- </div>
- </div>
- </p>
- </div><hr /><div class="manualnavbar" style="text-align: center;">
- <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-uh.quickstart.configuration.html">Setup</a></div>
- <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-uh.quickstart.proxy-installation.html">Installing a proxy</a></div>
- <div class="up"><a href="mysqlnd-uh.quickstart.html">Quickstart and Examples</a></div>
- <div class="home"><a href="index.html">PHP Manual</a></div>
- </div></body></html>