<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kamibu &#187; PHP</title>
	<atom:link href="http://blog.kamibu.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kamibu.com</link>
	<description></description>
	<lastBuildDate>Sat, 14 Nov 2009 10:51:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>RabbitEdit</title>
		<link>http://blog.kamibu.com/2008/10/04/rabbitedit/</link>
		<comments>http://blog.kamibu.com/2008/10/04/rabbitedit/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 19:37:40 +0000</pubDate>
		<dc:creator>Indy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/?p=65</guid>
		<description><![CDATA[RabbitEdit is a nifty tool that allows easier Rabbit development and saves you quite some time and effort. Its purpose is to automatically do particular common things for you, like writing pieces of code, creating parent directories for your new source files, and add the newly created files (and directories) into the SVN repository. It [...]]]></description>
			<content:encoded><![CDATA[<p>RabbitEdit is a nifty tool that allows easier Rabbit development and saves you quite some time and effort.<br />
Its purpose is to automatically do particular common things for you, like writing pieces of code, creating parent directories for your new source files, and add the newly created files (and directories) into the SVN repository.<br />
It can be found in rabbit/etc/rabbitedit/ and its installation is as simple as:</p>
<p><pre class="php"><span style="color: #808080; font-style: italic;"># make install </span></pre><br />
You can uninstall it similarly, running:</p>
<p><pre class="php"><span style="color: #808080; font-style: italic;"># make uninstall </span></pre><br />
Its configuration file can be found at /etc/rabbitedit/rabbitedit.conf and follows the common configuration rules used by the most programs.<br />
You can see how it works, by issuing:</p>
<p><pre class="php">$ rabbitedit --help</pre><br />
A simple example of its usage, could be the following:</p>
<p><pre class="php">$ rabbitedit --parents --svn libs/customer/settings.php</pre><br />
Let&#8217;s assume you are in the root directory of your rabbit project. By running the above, RabbitEdit creates libs/customer/ if it does not exist (this is indicated by the &#8220;&#8211;parents&#8221; option), adds that directory into the SVN repository (indicated by &#8220;&#8211;svn&#8221;) as well as libs/customer/settings.php, and if settings.php did not exist before, it creates it and writes a standard piece of code in it (having the information that the file is a lib, and is about the settings of customers). If the file existed before, it leaves it as it was. Then, it opens the file with the source code editor of your choice.</p>
<p>RabbitEdit is written in Python, and is designed with the hope it fastens up the development of Rabbit projects and gives developers the chance to avoid drudgery, and focus on really important things, like actually coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2008/10/04/rabbitedit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8220;echo&#8221; and &#8220;md5sum&#8221;</title>
		<link>http://blog.kamibu.com/2008/10/04/echo-and-md5sum/</link>
		<comments>http://blog.kamibu.com/2008/10/04/echo-and-md5sum/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 18:37:24 +0000</pubDate>
		<dc:creator>Indy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/?p=53</guid>
		<description><![CDATA[A problem I faced when I attempted to manually set a password for my user in Water, was that I could not login. I created the md5 sum of my password, running: $ echo mypassword&#124;md5sum and pasted it into the MySQL field of phpMyAdmin. After many login failures and enough searching about what is to [...]]]></description>
			<content:encoded><![CDATA[<p>A problem I faced when I attempted to manually set a password for my user in Water, was that I could not login. I created the md5 sum of my password, running:</p>
<p><pre class="php">$ <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> mypassword|md5sum</pre></p>
<p>and pasted it into the MySQL field of phpMyAdmin. After many login failures and enough searching about what is to blame, Kostis90gr found out that &#8220;echo&#8221;, is outputting a newline at the end of the string, so the md5 sum produced from the command above, was different than the one produced from PHP&#8217;s md5() function, simply because they were given different input. So, the right thing to do, is to pass &#8220;echo&#8221; the &#8220;-n&#8221; option, like:</p>
<p><pre class="php">$ <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> -n mypassword|md5sum</pre></p>
<p>which prevents &#8220;echo&#8221; from outputting a newline at the end of the string.<br />
I hope this post is helpful, because my unawareness of this &#8220;echo&#8221;&#8216;s little &#8220;particularity&#8221;, caused me quite enough frustration.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2008/10/04/echo-and-md5sum/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Overloading in PHP must use public methods</title>
		<link>http://blog.kamibu.com/2008/07/25/overloading-in-php-must-use-public-methods/</link>
		<comments>http://blog.kamibu.com/2008/07/25/overloading-in-php-must-use-public-methods/#comments</comments>
		<pubDate>Fri, 25 Jul 2008 17:55:10 +0000</pubDate>
		<dc:creator>dionyziz</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/?p=40</guid>
		<description><![CDATA[The magically overloading methods in PHP, such as __get, and __set must be public according to The PHP Manual. Aleksis recently pointed that out to me by making his new methods public, which gave me the initiative to look it up at the manual. I thought this script to convert them might end up being [...]]]></description>
			<content:encoded><![CDATA[<p>The magically overloading methods in PHP, such as __get, and __set must be public according to <a href="http://gr.php.net/manual/en/language.oop5.overloading.php">The PHP Manual</a>.</p>
<p><a href="http://www.kamibu.com/about/abresas">Aleksis</a> recently pointed that out to me by making his new methods public, which gave me the initiative to look it up at the manual.</p>
<p>I thought this script to convert them might end up being useful to others, too:<br />
<pre class="bash"><span style="color: #b1b100;">for</span> i <span style="color: #b1b100;">in</span> `find -iname <span style="color: #ff0000;">&quot;*.php&quot;</span>
               |xargs egrep -i <span style="color: #ff0000;">&quot;(private|protected) function __(get|set)&quot;</span>
               |awk -F: <span style="color: #ff0000;">'{print $1}'</span>`
<span style="color: #b1b100;">do</span> 
sed <span style="color: #ff0000;">'s/<span style="color: #000099; font-weight: bold;">\(</span>private<span style="color: #000099; font-weight: bold;">\|</span>protected<span style="color: #000099; font-weight: bold;">\)</span> function __<span style="color: #000099; font-weight: bold;">\(</span>get<span style="color: #000099; font-weight: bold;">\|</span>set<span style="color: #000099; font-weight: bold;">\)</span>/public function __<span style="color: #000099; font-weight: bold;">\2</span>/'</span> <span style="color: #0000ff;">$i</span> &gt;<span style="color: #0000ff;">$i</span>.processed;
mv <span style="color: #0000ff;">$i</span>.processed <span style="color: #0000ff;">$i</span>
<span style="color: #b1b100;">done</span></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2008/07/25/overloading-in-php-must-use-public-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Which &#8220;false&#8221; is it?</title>
		<link>http://blog.kamibu.com/2008/05/27/which-false-is-it/</link>
		<comments>http://blog.kamibu.com/2008/05/27/which-false-is-it/#comments</comments>
		<pubDate>Tue, 27 May 2008 18:38:02 +0000</pubDate>
		<dc:creator>dionyziz</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/?p=37</guid>
		<description><![CDATA[dionyziz@turing ~ $ cat test.php &#60;?php class false &#123; &#125; &#160; $c = New false&#40;&#41;; echo get_class&#40; $c &#41;; ?&#62; &#160; dionyziz@turing ~ $ php test.php false dionyziz@turing ~ $]]></description>
			<content:encoded><![CDATA[<p><pre class="php">dionyziz@turing ~ $ cat test.php
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$c</span> = <span style="color: #000000; font-weight: bold;">New</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <a href="http://www.php.net/get_class"><span style="color: #000066;">get_class</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$c</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
dionyziz@turing ~ $ php test.php
<span style="color: #000000; font-weight: bold;">false</span>
dionyziz@turing ~ $</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2008/05/27/which-false-is-it/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Change collation on all columns of a database</title>
		<link>http://blog.kamibu.com/2008/05/25/change-collation-on-all-columns-of-a-database/</link>
		<comments>http://blog.kamibu.com/2008/05/25/change-collation-on-all-columns-of-a-database/#comments</comments>
		<pubDate>Sun, 25 May 2008 10:51:08 +0000</pubDate>
		<dc:creator>dionyziz</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/?p=36</guid>
		<description><![CDATA[It was recently required for me to change the collation of each and every column of every table in a database from &#8216;latin1&#8242; to &#8216;utf8&#8242;. Although the table collations were correct, the column collations were incorrect. It&#8217;s a cumbersome process to perform manually, and there&#8217;s apparently no real automated way to do it without a [...]]]></description>
			<content:encoded><![CDATA[<p>It was recently required for me to change the collation of each and every column of every table in a database from &#8216;latin1&#8242; to &#8216;utf8&#8242;. Although the table collations were correct, the column collations were incorrect. It&#8217;s a cumbersome process to perform manually, and there&#8217;s apparently <a href="http://lists.mysql.com/mysql/186286">no real automated way to do it without a script</a>. Although collation information is only meta-data, not actual data, I found this problem interesting.</p>
<p>Changing one column collation information is easy enough to do with one MySQL query:</p>
<p><pre class="php">ALTER TABLE `moods` 
CHANGE `mood_label` `mood_label` text CHARACTER SET utf8 COLLATE utf8_unicode_ci;</pre></p>
<p>Changing all the columns is more difficult. Here&#8217;s a small script that I came up with to do it recently:</p>
<p><pre class="php">dionyziz@orion:~$ mysqldump -u root --password=<span style="color: #cc66cc;">1234</span> \ 
--no-data --no-create-db --<a href="http://www.php.net/compact"><span style="color: #000066;">compact</span></a> ccbeta \
|egrep <span style="color: #ff0000;">'CREATE TABLE|latin1'</span> \
|sed <span style="color: #ff0000;">'s/CREATE TABLE `<span style="color: #000099; font-weight: bold;">\(</span>.*<span style="color: #000099; font-weight: bold;">\)</span>` (/;ALTER TABLE `<span style="color: #000099; font-weight: bold;">\1</span>`/'</span> \
|sed <span style="color: #ff0000;">'s/character set latin1/CHARACTER SET utf8 COLLATE utf8_unicode_ci/'</span> \
|sed <span style="color: #ff0000;">'s/  `<span style="color: #000099; font-weight: bold;">\(</span>.*<span style="color: #000099; font-weight: bold;">\)</span>`/ CHANGE `<span style="color: #000099; font-weight: bold;">\1</span>` `<span style="color: #000099; font-weight: bold;">\1</span>`/'</span>&gt;columns
dionyziz@orion:~$ php -r <span style="color: #ff0000;">'file_put_contents( &quot;columns&quot;, 
    preg_replace( &quot;#^;|ALTER TABLE `.*`(<span style="color: #000099; font-weight: bold;">\\</span>s*;|$)#&quot;, &quot;&quot;, 
    preg_replace( &quot;#,(<span style="color: #000099; font-weight: bold;">\\</span>s*);#&quot;, &quot;;<span style="color: #000099; font-weight: bold;">\\</span>1&quot;, 
    file_get_contents( &quot;columns&quot; ) ) ) );'</span>
dionyziz@orion:~$ <a href="http://www.php.net/mysql"><span style="color: #000066;">mysql</span></a> -u root --password=<span style="color: #cc66cc;">1234</span> ccbeta &lt;columns</pre></p>
<p>Let&#8217;s go through it step-by-step.</p>
<p><pre class="php">mysqldump -u root --password=<span style="color: #cc66cc;">1234</span> --no-data --no-create-db --<a href="http://www.php.net/compact"><span style="color: #000066;">compact</span></a> ccbeta</pre></p>
<p>This creates a list of CREATE TABLE statements for all our tables. That&#8217;s good because it&#8217;ll allow us to determine whether the collation of a column is incorrect. Here&#8217;s an example CREATE TABLE statement:</p>
<p><pre class="php">CREATE TABLE `albums` <span style="color: #66cc66;">&#40;</span>
  `album_id` int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> NOT <span style="color: #000000; font-weight: bold;">NULL</span> auto_increment,
  `album_userid` int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> NOT <span style="color: #000000; font-weight: bold;">NULL</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #ff0000;">'0'</span>,
  `album_created` datetime NOT <span style="color: #000000; font-weight: bold;">NULL</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #ff0000;">'0000-00-00 00:00:00'</span>,
  `album_name` text character set latin1 NOT <span style="color: #000000; font-weight: bold;">NULL</span>,
  `album_description` text character set latin1 NOT <span style="color: #000000; font-weight: bold;">NULL</span>,
  PRIMARY <a href="http://www.php.net/key"><span style="color: #000066;">KEY</span></a>  <span style="color: #66cc66;">&#40;</span>`album_id`<span style="color: #66cc66;">&#41;</span>,
  <a href="http://www.php.net/key"><span style="color: #000066;">KEY</span></a> `album_userid` <span style="color: #66cc66;">&#40;</span>`album_userid` <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE=MyISAM AUTO_INCREMENT=<span style="color: #cc66cc;">55</span> <span style="color: #000000; font-weight: bold;">DEFAULT</span> CHARSET=utf8 COLLATE=utf8_unicode_ci;</pre></p>
<p>In this example, the `album_name` and `album_description` columns are wrong and need their collations changed.</p>
<p><pre class="php">egrep <span style="color: #ff0000;">'CREATE TABLE|latin1'</span></pre></p>
<p>This simple line limits our results to only lines that contain &#8220;CREATE TABLE&#8221; or &#8220;latin1&#8243;. That&#8217;s useful since it&#8217;ll only show the table names followed by a list of all incorrectly collated columns, if any. The result would be something like this:</p>
<p><pre class="php">CREATE TABLE `relations` <span style="color: #66cc66;">&#40;</span>
CREATE TABLE `searches` <span style="color: #66cc66;">&#40;</span>
 `search_query` text character set latin1 NOT <span style="color: #000000; font-weight: bold;">NULL</span>,
CREATE TABLE `shoutbox` <span style="color: #66cc66;">&#40;</span>
 `shout_text` text character set latin1 NOT <span style="color: #000000; font-weight: bold;">NULL</span>,
 `shout_delreason` text character set latin1 NOT <span style="color: #000000; font-weight: bold;">NULL</span>,</pre></p>
<p>(with more entries potentially)</p>
<p>Good. Now all we need to do is modify these lines to make them ALTER TABLE lines:</p>
<p><pre class="php">sed <span style="color: #ff0000;">'s/CREATE TABLE `<span style="color: #000099; font-weight: bold;">\(</span>.*<span style="color: #000099; font-weight: bold;">\)</span>` (/;ALTER TABLE `<span style="color: #000099; font-weight: bold;">\1</span>`/'</span></pre></p>
<p>Ah, the magic of regular expressions. This removes the final &#8220;(&#8221; of every CREATE TABLE line, as we don&#8217;t need it and also changes the word &#8220;CREATE&#8221; into &#8220;ALTER&#8221;. It also adds a semicolon in front of the ALTER TABLE statement (to terminate the previous statement).</p>
<p><pre class="php">sed <span style="color: #ff0000;">'s/character set latin1/CHARACTER SET utf8 COLLATE utf8_unicode_ci/'</span></pre></p>
<p>Straightforward enough, this replaces the existing character set instruction from latin1 to utf8, and adds the correct collation as well.</p>
<p><pre class="php">sed <span style="color: #ff0000;">'s/  `<span style="color: #000099; font-weight: bold;">\(</span>.*<span style="color: #000099; font-weight: bold;">\)</span>`/ CHANGE `<span style="color: #000099; font-weight: bold;">\1</span>` `<span style="color: #000099; font-weight: bold;">\1</span>`/'</span></pre></p>
<p>Finally, this adds the word &#8220;CHANGE&#8221; in front of every column line and repeats the column name (as we want to tell MySQL which column to change (first repetition) and to which to change it (second repetition)). The result is:</p>
<p><pre class="php">;ALTER TABLE `relations`
;ALTER TABLE `searches`
 CHANGE `search_query` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT <span style="color: #000000; font-weight: bold;">NULL</span>,
;ALTER TABLE `shoutbox`
 CHANGE `shout_text` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT <span style="color: #000000; font-weight: bold;">NULL</span>,
 CHANGE `shout_delreason` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT <span style="color: #000000; font-weight: bold;">NULL</span>,</pre></p>
<p>Pretty close to what we actually want. You&#8217;ll notice three problems:</p>
<ul>
<li>There are empty ALTER statements</li>
<li>There&#8217;s an extra comma at the end of every column (providing all your tables have a primary key, as they should)</li>
<li>There&#8217;s a redundant semicolon at the beginning</li>
</ul>
<p>These problems cannot easily be fixed by sed because sed performs a line-to-line processing. A sed expert might have been able to provide us with a better solution, but I&#8217;ll prefer to use the PREG feature of PHP. To use PHP, first let&#8217;s save our current result into a file:</p>
<p><pre class="php">&gt;columns</pre></p>
<p>Time to run our PHP code on the target file:</p>
<p><pre class="php">php -r <span style="color: #ff0000;">'file_put_contents( &quot;columns&quot;, 
    preg_replace( &quot;#^;|ALTER TABLE `.*`(<span style="color: #000099; font-weight: bold;">\\</span>s*;|$)#&quot;, &quot;&quot;, 
    preg_replace( &quot;#,(<span style="color: #000099; font-weight: bold;">\\</span>s*);#&quot;, &quot;;<span style="color: #000099; font-weight: bold;">\\</span>1&quot;, 
    file_get_contents( &quot;columns&quot; ) ) ) );'</span></pre></p>
<p>Let&#8217;s analyze it in short.</p>
<p><pre class="php"><a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;columns&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>This, simply enough, reads the &#8220;columns&#8221; file into memory. Now we&#8217;ll perform two regular expression replacements:</p>
<p>First, we&#8217;ll match the following regular expression:</p>
<p><pre class="php"><span style="color: #808080; font-style: italic;">#,(\s*);# </span></pre></p>
<p>(notice that the # are separators that wrap the regular expression for clarity &#8212; they aren&#8217;t part of the actual regular expression)</p>
<p>Anything matching this will be replaced by ;\1. This means that a comma followed by any whitespace (including a new line) followed by a semicolon will be replaced by only a semicolon (and the same whitespace). This simply removes the redundant comma at the end of every ALTER statement.</p>
<p>Second, we&#8217;ll match the following:</p>
<p><pre class="php"><span style="color: #808080; font-style: italic;">#^;|ALTER TABLE `.*`(\\s*;|$)# </span></pre></p>
<p>Anything matching will be removed. You&#8217;ll notice that this regular expression matches basically two things (separated by the first alternation (pipe) character). </p>
<p>The first part is:</p>
<p><pre class="php"><span style="color: #808080; font-style: italic;">#^;# </span></pre></p>
<p>It&#8217;ll remove the first line if it only contains a single semicolon (which it does in our example). </p>
<p>The second part is:</p>
<p><pre class="php"><span style="color: #808080; font-style: italic;">#ALTER TABLE `.*`(\\s*;|$)# </span></pre></p>
<p>This will look for empty ALTER TABLE statements (an ALTER TABLE statement followed only by whitespace and a semicolon or an end-of-file) and remove them. </p>
<p>Finally, we&#8217;ll write the result back to the file we read from:</p>
<p><pre class="php">file_put_contents<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;columns&quot;</span>, ... <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>Now if we <em>cat </em> that file we&#8217;ll see that it contains all ALTER statements in the form we want them:</p>
<p><pre class="php">ALTER TABLE `searches`
 CHANGE `search_query` `search_query` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT <span style="color: #000000; font-weight: bold;">NULL</span>;
ALTER TABLE `shoutbox`
 CHANGE `shout_text` `shout_text` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT <span style="color: #000000; font-weight: bold;">NULL</span>,
 CHANGE `shout_delreason` `shout_delreason` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT <span style="color: #000000; font-weight: bold;">NULL</span>;</pre></p>
<p>Excellent. Finally, let&#8217;s execute it:</p>
<p><pre class="php"><a href="http://www.php.net/mysql"><span style="color: #000066;">mysql</span></a> -u root --password=<span style="color: #cc66cc;">1234</span> ccbeta &lt;columns</pre></p>
<p>You can also add &#8216;time&#8217; in front of it to measure how long it&#8217;ll take. We can now validate that the collations were changed successfully by, again, performing our initial dump and grepping for &#8216;latin1&#8242; to confirm that there are none.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2008/05/25/change-collation-on-all-columns-of-a-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Increasing method visibility through extension</title>
		<link>http://blog.kamibu.com/2008/03/16/increasing-method-visibility-through-extension/</link>
		<comments>http://blog.kamibu.com/2008/03/16/increasing-method-visibility-through-extension/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 16:44:48 +0000</pubDate>
		<dc:creator>dionyziz</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/2008/03/16/increasing-method-visibility-through-extension/</guid>
		<description><![CDATA[Although it&#8217;s nothing superspecial, I wanted to illustrate how it is possible to modify the visibility of a parent class method when extending it, to convert it from &#8216;protected&#8217; to &#8216;public&#8217;. Assume we have a base class which exposes a protected function called &#8216;Test&#8217;. We want to extend the class and make that function public [...]]]></description>
			<content:encoded><![CDATA[<p>Although it&#8217;s nothing superspecial, I wanted to illustrate how it is possible to modify the visibility of a parent class method when extending it, to convert it from &#8216;protected&#8217; to &#8216;public&#8217;. Assume we have a base class which exposes a protected function called &#8216;Test&#8217;. We want to extend the class and make that function public in our extension. Here&#8217;s how:</p>
<p><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #000000; font-weight: bold;">class</span> Fred <span style="color: #66cc66;">&#123;</span>
        protected <span style="color: #000000; font-weight: bold;">function</span> Test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            ?&gt;Boo!<span style="color: #000000; font-weight: bold;">&lt;?php</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">class</span> Barney extends Fred <span style="color: #66cc66;">&#123;</span>
        public <span style="color: #000000; font-weight: bold;">function</span> Test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            parent::<span style="color: #006600;">Test</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$barney</span> = <span style="color: #000000; font-weight: bold;">New</span> Barney<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$barney</span>-&gt;<span style="color: #006600;">Test</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></p>
<p>Notice that $barney->Test() is a public method call. It&#8217;s also possible to disallow this kind of behavior from the parent&#8217;s perspective by declaring a method as final. This way it can no longer be overridden (including its visibility):</p>
<p><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #000000; font-weight: bold;">class</span> Fred <span style="color: #66cc66;">&#123;</span>
        final protected <span style="color: #000000; font-weight: bold;">function</span> Test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            ?&gt;Boo!<span style="color: #000000; font-weight: bold;">&lt;?php</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2008/03/16/increasing-method-visibility-through-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql fetch array problem</title>
		<link>http://blog.kamibu.com/2007/06/24/mysql-fetch-array-problem/</link>
		<comments>http://blog.kamibu.com/2007/06/24/mysql-fetch-array-problem/#comments</comments>
		<pubDate>Sun, 24 Jun 2007 10:08:15 +0000</pubDate>
		<dc:creator>rhapsody</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/2007/06/24/mysql-fetch-array-problem/</guid>
		<description><![CDATA[Currently I&#8217;m working on converting my practice project, a simple uploader program in PHP, into OOP. Let&#8217;s take a look at the construct of the User class. For every instance of the class that is created, there is a check on whether the parameter provided is an id or a username, simply by checking if [...]]]></description>
			<content:encoded><![CDATA[<p>Currently I&#8217;m working on converting my practice project, a simple uploader program in PHP, into OOP.<br />
Let&#8217;s take a look at the construct of the User class. For every instance of the class that is created, there is a check on whether the parameter provided is an id or a username, simply by checking if it&#8217;s a string or an integer. If it&#8217;s an integer it supposes that the variable given is a user id, and so continues by filling up the other properties.</p>
<p><pre class="php"><span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #66cc66;">&#123;</span>
&nbsp;
private <span style="color: #0000ff;">$mId</span>;
private <span style="color: #0000ff;">$mName</span>;
......
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$construct</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/is_int"><span style="color: #000066;">is_int</span></a><span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$construct</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>  <span style="color: #808080; font-style: italic;">// mySQL query by user id</span>
&nbsp;
    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">mId</span> = <span style="color: #0000ff;">$construct</span>;
    .......
		
  <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>                            <span style="color: #808080; font-style: italic;">// mySQL query by username</span>
&nbsp;
    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">mName</span> = <span style="color: #0000ff;">$construct</span>;
     .......</pre></p>
<p>In order to display the username which is a private property ($mName) I used a simple fuction<br />
<pre class="php">public <span style="color: #000000; font-weight: bold;">function</span> GetName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">mName</span>;
<span style="color: #66cc66;">&#125;</span></pre></p>
<p>So in my index.php I used to following code to display the user who owned a specific folder:<br />
<pre class="php"><span style="color: #0000ff;">$owner</span> = <span style="color: #000000; font-weight: bold;">New</span> User<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$folder_owner</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p><em>$folder_owner['id']</em> was part of an mySQL fetched array which carried the owner&#8217;s id.</p>
<p>However, when I called to my previous method to display the username:<br />
<pre class="php"><a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$owner</span>-&gt;<span style="color: #006600;">GetName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre><br />
the result was echoing not the username as expected but instead the user&#8217;s id.<br />
<pre class="php">foo.txt, created on <span style="color: #cc66cc;">12</span><span style="color: #cc66cc;">-06</span><span style="color: #cc66cc;">-2007</span> by <span style="color: #cc66cc;">2</span></pre><br />
The class&#8217;s methods seem correct, so what was it that went wrong back there?<br />
Let&#8217;s look again the creation of my new User object.<br />
<pre class="php"><span style="color: #0000ff;">$owner</span> = <span style="color: #000000; font-weight: bold;">New</span> User<span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">$folder_owner</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
As I said before, <em>$folder_owner['id']  </em>came from a mysql_fetch_array function. When this function is called, the array created from each row is always an array of strings. So even if the cell that contains the requested id in my db contains an integer, the result will always be extracted as a string. As my constructor received a string and not an integer,  <em>$this->mName</em> was set with the variable provided, which in our case was in fact the id and not the username, but still had the form of a string. So, calling at the method GetName() would always return the id.</p>
<p>The error was pointed out by my friend and tutor Dionyziz, who also suggested me the simple solution of converting <em>$folder_owner['id'] </em>to an integer before using it as a parameter for my method.<br />
<pre class="php"><span style="color: #0000ff;">$owner</span> = <span style="color: #000000; font-weight: bold;">New</span> User<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> integer <span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$folder_owner</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>What will definitely not be forgotten:</p>
<ul>
<li><strong>mysql_fetch_array()</strong> always returns an array of strings.</li>
</ul>
<p>special thanks to Dionyziz for helping me out <img src='http://blog.kamibu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>More info:</p>
<p><a href="http://fr3.php.net/mysql_fetch_array">mysql_fetch_array()</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2007/06/24/mysql-fetch-array-problem/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

