<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Behind The View</title>
	
	<link>http://www.behindtheview.com</link>
	<description>Development blog about PHP, coding and everything behind the view</description>
	<lastBuildDate>Sat, 09 Jan 2010 13:30:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/BehindTheView" /><feedburner:info uri="behindtheview" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Class Aliases</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/qOXNLbF5fSc/</link>
		<comments>http://www.behindtheview.com/2010/01/08/class-aliases/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 18:56:50 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aliasing]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[class_alias]]></category>
		<category><![CDATA[new year]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[static]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=161</guid>
		<description><![CDATA[I&#8217;ve been very busy lately, for example with the first release of Mai Ocean, that it was hard to find time for writing posts.   Of course I had to give some sign of life! Therefore I am filling this first post of the year with a great function I recently found.
When writing the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been very busy lately, for example with the first release of Mai Ocean, that it was hard to find time for writing posts. <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  Of course I had to give some sign of life! Therefore I am filling this first post of the year with a great function I recently found.</p>
<p>When writing the handler system for Mai Ocean I had to find a way to access a static class that had a variable name. Accidentally I found the new <em><a href="http://nl3.php.net/manual/en/function.class-alias.php">class_alias()</a></em> function on php.net. This function gives you the ability to easily create a alias for any class. Usage is simple:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">class_alias<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'New'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Original'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The function is available in version 5.3 and above and works with namespaces. In Mai Ocean I just use the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$handler</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'MaiOcean\UserHandler\\'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">config</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handlers</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
&nbsp;
class_alias<span style="color: #009900;">&#40;</span><span style="color: #000088;">$handler</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'MaiOcean\UserHandler\Handler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now I can just use my new name instead of a variable! How great is that? <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/qOXNLbF5fSc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2010/01/08/class-aliases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2010/01/08/class-aliases/</feedburner:origLink></item>
		<item>
		<title>Multiprocessing part 1: Quick &amp; Dirty</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/3GY--0S3oto/</link>
		<comments>http://www.behindtheview.com/2009/12/09/multiprocessing-part-1-quick-and-dirty/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 21:35:20 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[background processing]]></category>
		<category><![CDATA[child]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[exec]]></category>
		<category><![CDATA[multiprocessing]]></category>
		<category><![CDATA[parent]]></category>
		<category><![CDATA[pcntl]]></category>
		<category><![CDATA[PHP CLI]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[proc_open]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=158</guid>
		<description><![CDATA[p>PHP can do only one thing at a time. Usually (for websites) this isn&#8217;t a big problem because you only have to process a request. On the other hand, if you are doing something more time-consuming PHP could be to slow.
An example is my chat bot server (Mai Ocean). For each conversation I have to [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_159" class="wp-caption alignleft" style="width: 210px"><img src="http://www.behindtheview.com/wp-content/uploads/2009/12/processor-300x300.jpg" alt="DEC Alpha AXP 21064 microprocessor" title="processor" width="200" height="200" class="size-medium wp-image-159" /><p class="wp-caption-text">DEC Alpha AXP 21064 microprocessor</p></div>
<p>PHP can do only one thing at a time. Usually (for websites) this isn&#8217;t a big problem because you only have to process a request. On the other hand, if you are doing something more time-consuming PHP could be to slow.</p>
<p>An example is my chat bot server (Mai Ocean). For each conversation I have to make a connection with a server, so for each conversation I have a socket that I have to listen and write to. So if 10 people are talking to my bot I have at least 11 socket connections (10 conversations + main server), you could imagine that this would be way to much for just one script. So how can we solve this?<span id="more-158"></span></p>
<p>With multiprocessing! Multiprocessing is a method to run <em>multiple processes</em> and give each of them a task to do. In the following weeks I am going to write about different ways to implement multiprocessing:</p>
<ol>
<li>Part 1: Quick &#038; Dirty (using exec())</li>
<li>Part 2: Parent &#038; Childs (using PCNTL)</li>
<li>Part 3: Communication (using proc_open())</li>
</ol>
<p>Today I will cover the first part and will talk about the easiest method using <em>exec()</em>. This method is very simple but you cannot do much with it. Anyway, lets begin: First of all we should create a parent process. No matter what method you use you always have to create a parent. The parent process creates all the child processes and can differ from its children but can also be the same.</p>
<p>Today our child processes differs from the parent because we have to add some code to our parent to start up its children. A simple diagram of our program could look like this:</p>
<p><img src="https://cacoo.com/diagrams/P3un3w5rqzFa60Vd-75F98.png"/></p>
<p>As you see we have one parent process and three children. For this article I will use only one child script to make it easier. So before we look at creating child processes we first have to write a child script. Your child script can do and contain anything you want but it must be possible to run it from the command line.</p>
<p>For the rest of the article I will be using the following child script and name it <em>child.php</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #990000;">date_default_timezone_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Europe/Amsterdam'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The child will first wait for two seconds and then output the current time. You should note that this child exits by itself, that is something to remember for next time. Now we have a child we can create the parent script. In this example the parent should only start its children and then exit:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Starting child <span style="color: #006699; font-weight: bold;">$i</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #990000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php child.php&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now save the code (for example as <em>parent.php</em>) and run it from the command line. If everything works you should see something like the following:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ php test.php
Starting child <span style="color: #000000;">0</span>
<span style="color: #000000;">20</span>:<span style="color: #000000;">34</span>:00
Starting child <span style="color: #000000;">1</span>
<span style="color: #000000;">20</span>:<span style="color: #000000;">34</span>:02
Starting child <span style="color: #000000;">2</span>
<span style="color: #000000;">20</span>:<span style="color: #000000;">34</span>:04
Starting child <span style="color: #000000;">3</span>
<span style="color: #000000;">20</span>:<span style="color: #000000;">34</span>:06
Starting child <span style="color: #000000;">4</span>
<span style="color: #000000;">20</span>:<span style="color: #000000;">34</span>:08</pre></div></div>

<p>Now how does this work? The parent uses the <a href="http://php.net/manual/en/function.exec.php">exec()</a> function to start up a child. As a parameter we used the command <em>php</em> and the name of our child <em>child.php</em>. exec() will run the command and return the last line from the result. In our example this is the current time. Of course you could also add some arguments to the command to interact with the child script.</p>
<p>If you look closely you see that there is an 2 seconds interval between each start of a child. This is caused by the sleep function of each child. In other words our parent script has to wait for the child to complete before it can continue. When you need multiprocessing you do not want this because you do not have any profit from it. So how can we fix this? As stated on php.net:</p>
<blockquote><p><em>If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.</em></p></blockquote>
<p>Therefore if we want to let our children run in the background we have to direct the output, this can easily be done by changing the command:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php child.php &gt; /dev/null &amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code above will send all output to <em>/dev/null</em> and there it will disappear. When you use this method the script runs in the background and will not let the parent wait. The parent will start 5 children without waiting for the first to finish.</p>
<p><strong>NOTE: The method above does not work on Windows!</strong> It will only work on Unix systems. If you want to use it on Windows you should use another method. A possible solution is replacing the exec() command with <em>pclose(popen("start /B ". $cmd, "r"));</em> but I did not test it myself.</p>
<p>What do we have so far? We have a parent script that starts up 5 children that run in the background. The children in this case are useless because they output to /dev/null and you will never see that. Nevertheless you should be able to create more useful child scripts for yourself. For example: each script could backup a different database or file.</p>
<p>With the above you should be able to create programs that implement a form of simplistic multiprocessing. To make it all a bit better (and more difficult) we should let the parent and children communicate with each other. There is only one problem: the method we used here is not really useful when we need communication. This is because we cannot interact with the scripts directly, to do this we need another method (which I am going to discuss in the following articles).</p>
<p>If you still want to use this method (because you have not yet read my following article or I didn't finish it yet <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) you can use files. For example (from a previous version of my bot server) a conversation script checked every 2 seconds if there was a 'server-stop' file present in the temp directory of my application. When the file was present the child would close the socket and exit by itself. The reason why I am not going to explain this is because it is ugly and I want to forget that I every used it <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> .</p>
<p>So in conclusion, we have looked at <em>exec()</em> which is a function to execute commands and used it to start child processes. This way is useful if you need it fast and do not have the time to create a better method. In the next part I am going to explain a more advanced method of creating parent and child processes.</p>
<p>See you next time! <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/3GY--0S3oto" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2009/12/09/multiprocessing-part-1-quick-and-dirty/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2009/12/09/multiprocessing-part-1-quick-and-dirty/</feedburner:origLink></item>
		<item>
		<title>PHP, Hell or Heaven: Documentation</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/XOLv0oDxS0c/</link>
		<comments>http://www.behindtheview.com/2009/12/05/php-hell-or-heaven-documentation/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 11:53:44 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP, Hell or Heaven]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[hell or heaven]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[php.net]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=152</guid>
		<description><![CDATA[ programming language can be great but if you do not know how it works then it&#8217;s (practically) worthless. In general you can say that the documentation is a important part of a programming language. Returning to this series, we can only tag PHP as &#8220;Heaven&#8221; if it has a good documentation. Now that&#8217;s the [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_146" class="wp-caption alignleft" style="width: 224px"><img src="http://www.behindtheview.com/wp-content/uploads/2009/11/hellorheaven-214x300.png" alt="Hell or Heaven?" title="hellorheaven" width="214" height="300" class="size-medium wp-image-146" /><p class="wp-caption-text">Hell or Heaven?</p></div>A programming language can be great but if you do not know how it works then it&#8217;s (practically) worthless. In general you can say that the documentation is a important part of a programming language. Returning to this series, we can only tag PHP as &#8220;Heaven&#8221; if it has a good documentation. Now that&#8217;s the question we should answer today.</p>
<p>If you have used PHP before you should already now the answer to this question. It is <strong>definitely yes</strong>. I have never heard someone saying that the documentation was bad or somehow not good enough. Even people who hate PHP can&#8217;t disprove that (of course they could try). <span id="more-152"></span></p>
<p>Anyway, why is it so good? I think it&#8217;s most important aspect is it&#8217;s completeness. If I&#8217;m writing C++ and I want to know something about a function or the language in general I go to Google. When I need to know something about PHP I go to (you should know this) <a href="http://www.php.net">php.net</a>. I do this for one simple reason: the PHP documentation contains all the information I need. Even when that is not enough, each page has a lot of user contributed notes to help you even further. I only need Google when I&#8217;m searching for a tutorial or more in-depth information about a topic.</p>
<p><div id="attachment_157" class="wp-caption aligncenter" style="width: 589px"><img src="http://www.behindtheview.com/wp-content/uploads/2009/12/phpdocumentation3.png" alt="PHP Documentation" title="phpdocumentation" width="579" height="324" class="size-full wp-image-157" /><p class="wp-caption-text">PHP Documentation</p></div>
<p>The PHP documentation itself contains a full list of <a href="http://www.php.net/manual/en/funcref.php">functions</a>, information about the <a href="http://www.php.net/manual/en/langref.php">language itself</a>, <a href="http://www.php.net/manual/en/security.php">security information</a>, etcetera, etcetera.. So in a few words: it contains a lot! <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In comparison to a few other languages: C++ has <a href="http://www.cplusplus.com/">cplusplus.com</a> and <a href="http://java.sun.com/reference/index.jsp">Sun</a> gives information about Java. More than once I concluded that these documentations where not the answer to my problem and I had to search on the web. Of course this doesn&#8217;t say that these languages are bad. It just says that developing an application with a language that doesn&#8217;t have a good documentation could take much longer.</p>
<p>You should know that I am not saying that PHP is good because it has the best documentation of the world, I am just saying that the good documentation makes PHP a better language <em>to use</em>. I do not know if PHP has the best documentation but it is definitely one of the best. (Another language with a good documentation is <a href="http://www.python.org/doc/">Python</a>.) The documentation is something what some other languages lack and maybe should get more attention.</p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
<p>(Combining the <a href="http://www.behindtheview.com/2009/11/27/php-hell-or-heaven-data-types/">previous article</a> and this one we could say the score is 1 -1. PHP may have lost the previous one but the documentation surely gets it back into the race!)</p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/XOLv0oDxS0c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2009/12/05/php-hell-or-heaven-documentation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2009/12/05/php-hell-or-heaven-documentation/</feedburner:origLink></item>
		<item>
		<title>PHP, #3 Most popular programming language</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/oQ5svJUxxTo/</link>
		<comments>http://www.behindtheview.com/2009/12/02/php-number-3-programming-langauge/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 15:56:25 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[popularity]]></category>
		<category><![CDATA[ranking]]></category>
		<category><![CDATA[TIOBE]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=149</guid>
		<description><![CDATA[The comany TIOBE collects statistics about the popularity of programming languages. In their last analysis PHP was on position 3 and that is something to be proud of.   The only two that are popular than PHP are C and Java. PHP is also in the race for &#8220;Programming Language of 2009&#8243; together with [...]]]></description>
			<content:encoded><![CDATA[<p>The comany <a href="http://www.tiobe.com">TIOBE</a> collects statistics about the popularity of programming languages. In their last analysis PHP was on position 3 and that is something to be proud of. <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  The only two that are popular than PHP are C and Java. PHP is also in the race for &#8220;Programming Language of 2009&#8243; together with C and C#!</p>
<p>The development in the rankings can be found in the graph below:</p>
<div id="attachment_150" class="wp-caption alignleft" style="width: 586px"><img src="http://www.behindtheview.com/wp-content/uploads/2009/12/tpci_trends.png" alt="Long term trends for the top 10 programming languages" title="tpci_trends" width="576" height="432" class="size-full wp-image-150" /><p class="wp-caption-text">Long term trends for the top 10 programming languages</p></div>
<p>The total ranking list can be seen on the <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">TIOBE website</a>.</p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/oQ5svJUxxTo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2009/12/02/php-number-3-programming-langauge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2009/12/02/php-number-3-programming-langauge/</feedburner:origLink></item>
		<item>
		<title>The bootstrap: Germination of your application</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/-R3BNOdgDjc/</link>
		<comments>http://www.behindtheview.com/2009/12/01/the-bootstrap-germination-of-your-application/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 20:55:06 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Mai Ocean]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[assertions]]></category>
		<category><![CDATA[assert_options]]></category>
		<category><![CDATA[autoloading]]></category>
		<category><![CDATA[bootstrap]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[controllers]]></category>
		<category><![CDATA[germination]]></category>
		<category><![CDATA[ini_set]]></category>
		<category><![CDATA[Mai]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Zend_Config]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=137</guid>
		<description><![CDATA[After the start of your (PHP) application it will grow. You will include more and more files, execute more functions and handle more data. When you are finished processing, all memory will be released and your app will &#8216;die&#8217;. Summarized you could represent your application as something simple like a plant!
The first part of &#8220;it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_138" class="wp-caption alignleft" style="width: 202px"><a href="http://en.wikipedia.org/wiki/File:Sunflower_seedlings.jpg"><img class="size-medium wp-image-138" title="685px-Sunflower_seedlings" src="http://www.behindtheview.com/wp-content/uploads/2009/11/685px-Sunflower_seedlings-300x262.jpg" alt="Sunflower seedlings" width="192" height="167" /></a><p class="wp-caption-text">Sunflower seedlings</p></div>
<p>After the start of your (PHP) application it will grow. You will include more and more files, execute more functions and handle more data. When you are finished processing, all memory will be released and your app will &#8216;die&#8217;. Summarized you could represent your application as something simple like a plant!</p>
<p>The first part of &#8220;it&#8217;s life&#8221; is <a href="http://en.wikipedia.org/wiki/Germination">germination</a>, your application has to start and set up everything he needs. In more technical terms you can call this term the <em>bootstrap</em>. If you use a framework such as Zend Framework you&#8217;ve probably already heard from it. Using a .htacess file you direct all traffic to your bootstrap that then will startup the main application. For a person like I (who creates PHP CLI apps) the bootstrap is a way of setting important environment variables. So what should a bootstrap do? <span id="more-137"></span>In my view the most important tasks are:</p>
<ol>
<li>Setting application environment</li>
<li>Configuring the PHP environment</li>
<li>Configuring autoloading</li>
<li>Loading configurations</li>
<li>Starting the main application</li>
</ol>
<p>I doesn&#8217;t seem like much but if you use them properly a bootstrap can be really helpfull. Moreover, if you (like me) only program using objects (OOP) you need a file that starts your main class so you need a bootstrap anyway.</p>
<p>Now I want to further explain these 5 components of the bootstrap using a live example: the bootstrap of Mai Ocean. I will try to keep it as general as possible so I won&#8217;t use a framework. Of course I only give my view, the best bootstrap is the one that fits you and your app so after reading this post go create your own and share it if you like! <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>1. Setting application environment</h3>
<p>let&#8217;s start with the first part: the application environment. For all my applications I start with some &#8216;<strong>application constants</strong>&#8216;. I use these to define global information about the application. For example, Mai Ocean has the following constants:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APP_NAME'</span><span style="color: #339933;">,</span>		<span style="color: #0000ff;">'Mai Ocean'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APP_VERSION'</span><span style="color: #339933;">,</span>		<span style="color: #0000ff;">'0.1.0trunk'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APP_STAGE'</span><span style="color: #339933;">,</span>		<span style="color: #0000ff;">'development'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Because they all start with the prefix &#8220;APP&#8221; I can easily identify and use them throughout the application. The first two I only use for informational purposses and are not needed for the application. For example: bots created with Mai Ocean show the name and the version of the application in their private message. The third constant (APP_STAGE) is the most important and is actually used. I split config files in multiple sections and this constant defines what section should be loaded (in this case the development section). You may think: &#8220;This is pretty simple..&#8221; and you are absolutely right but constants like these can be really useful.</p>
<p>Because I use autoloading I have to define <strong>paths to my models, controllers and other classes</strong> to make it work. These paths are part of my application so I define them (just like the application constants) with constants. Due to this I can always use a constant if I want to use a path, then when I want to move something I only have to change the constant. For Mai Ocean I use the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PATH_ROOT'</span><span style="color: #339933;">,</span>		__DIR__<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PATH_LIBRARY'</span><span style="color: #339933;">,</span>		__DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/library'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PATH_EZCOMPONENTS'</span><span style="color: #339933;">,</span>	__DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/library/eZComponents'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PATH_APPLICATION'</span><span style="color: #339933;">,</span>	__DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/application'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PATH_CONTROLLERS'</span><span style="color: #339933;">,</span>	__DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/application/controllers'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PATH_MODELS'</span><span style="color: #339933;">,</span>		__DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/application/models'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PATH_HANDLERS'</span><span style="color: #339933;">,</span>		__DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/application/handlers'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>After this I can set my include path. As you might already know, the order of your include path is important because they are searched. PHP will search them in the order you&#8217;ve set them so set the most important directories first. I usually start with my application and models dir because it contains the most files I use:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span>
	PATH_APPLICATION 	<span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>
	PATH_MODELS		<span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>
	PATH_LIBRARY		<span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>
	PATH_EZCOMPONENTS	<span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>
	PATH_HANDLERS		<span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>	
	<span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>2. Tweaking the PHP environment</h3>
<p>The first part was pretty simple so lets continue. PHP has a large config file (php.ini) and especially when you write applications that will be used on multiple installations you should not trust them. If you rely on some config values you should set them yourself. The best place for that is (you could have guessed it) your bootstrap!</p>
<p>Probably the most important settings you should configure are the error settings. Of course you can change these with <em>error_reporting()</em> but I like to use <em>ini_set()</em> instead. When developing you want to show errors on screen, this is easier than reading a log when something goes wrong. Nevertheless, when you are ready for production you do not want to show errors any more. Instead of displaying them on screen you can easily save them to a log file. You could also create a custom error handler that would send an email to you or something like that. Anyway to set the error settings you could use the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'display_errors'</span><span style="color: #339933;">,</span> 	<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Do not display errors on screen</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error_reporting'</span><span style="color: #339933;">,</span> 	<span style="color: #009900; font-weight: bold;">E_ALL</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_NOTICE</span> <span style="color: #339933;">|</span> <span style="color: #009900; font-weight: bold;">E_STRICT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Show ALL errors</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'log_errors'</span><span style="color: #339933;">,</span> 		<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Log errors</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error_log'</span><span style="color: #339933;">,</span> 		PATH_ROOT <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/log/error.log'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Path of the error log</span></pre></div></div>

<p>Some other settings I use often are <em>memory_limit</em> (to handle a lot of data) and <em>max_execution_time</em> (to enable scripts that run forever). All settings you can set can be found at the PHP Manual: <a href="http://.php.net/manual/en/ini.list.php">http://php.net/manual/en/ini.list.php</a>.</p>
<p>Besides errors I also always configure assertions are handled. I use them a lot and configuring them makes them even more useful. I&#8217;d already written down some of the possible configuration in my post about assertions (<a href="http://www.behindtheview.com/2009/11/24/assertions-protection-against-yourself/">Assertions, protection against yourself</a>) but wrote down my full configuration list below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_ACTIVE<span style="color: #339933;">,</span> 		<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Enable assertions</span>
<span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_BAIL<span style="color: #339933;">,</span>			<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Terminate execution on failed assertions</span>
<span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_WARNING<span style="color: #339933;">,</span>		<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Issue a PHP warning for each failed assertion</span>
<span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_QUIET_EVAL<span style="color: #339933;">,</span>	<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Do not disable error_reporting during assertion expression evaluation</span>
<span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_CALLBACK<span style="color: #339933;">,</span>		<span style="color: #0000ff;">'someFunction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Some function to handle assertions (don't have one yet)</span></pre></div></div>

<p>This configuration I use when developing because I want to see when an assertion fails. On production I would disable warnings and let my callback handle the error.</p>
<p>Now there is only one thing left we need to configure. When you use date/time functions you may have already have seen this:</p>
<blockquote><p><em>Note: Since PHP 5.1.0 (when the date/time functions were rewritten), every call to a date/time function will generate a E_NOTICE if the timezone isn&#8217;t valid, and/or a E_WARNING message if using the system settings or the TZ environment variable.</em></p></blockquote>
<p>So we have to set a valid timezone, this is really easy. Just add the following to your bootstrap:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">date_default_timezone_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Europe/Amsterdam'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>3. Configuring autoloading</h3>
<p>I can be short at this: autoloading is great! <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  I am not going to explain how autoloading works because it is pretty straightforward. I use <em>Zend_Loader_Autoloader</em> as a base who uses the autoloader of eZ Components and also my own autoloading algorithm. In my bootstrap I only have to do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Get the Mai Ocean autoloader</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Autoloader.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Create a new autoloader for this application</span>
<span style="color: #000088;">$autoloader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MaiOcean\Autoloader<span style="color: #339933;">;</span></pre></div></div>

<h3>4. Loading configurations</h3>
<p>My controllers, models and other objects use configurations who I store in ini-files. These files are loaded and parsed in my bootstrap because I do not want to define them into my objects. Also, (unit) testing will be a lot easier if you can insert custom configurations.<br />
My favourite method is creating a config creator object that combines multiple ini-files in one Zend_Config object.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Create a config object to load all config files</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MaiOcean\Models\Config<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addModule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'application/default'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addModule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'users/development'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createConfig</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the example above I load 2 ini-files: the default configurations and the development user data. The last line will return a new Zend_Config object that contains all my configurations. </p>
<h3>5. Starting the main application</h3>
<p>You may have noticed but we have reached the last part: starting the main application. This is important but also the easiest part. If you use a framework you will probably dispatch a front controller. In my case (a CLI application) I just start up the main controller:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Create and run the controller for the Mai Ocean application</span>
<span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MaiOcean\Controller\MaiOcean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>The end..?</h3>
<p>We have reached the end of the bootstrap and of my article. I hope you&#8217;ve learnt something about my way of creating bootstraps. I&#8217;m very curious about your ideas so let me know! The full bootstrap of Mai Ocean can be found at <a href="http://bazaar.launchpad.net/~wouterbulten/mai-ocean/main/annotate/head%3A/mai-ocean.php">launchpad</a>. </p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/-R3BNOdgDjc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2009/12/01/the-bootstrap-germination-of-your-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2009/12/01/the-bootstrap-germination-of-your-application/</feedburner:origLink></item>
		<item>
		<title>Interesting topics</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/srPy9KS3QUA/</link>
		<comments>http://www.behindtheview.com/2009/11/29/interesting-topics/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 22:26:05 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[About Behind The View]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[topics]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=148</guid>
		<description><![CDATA[When I started this blog I already had some topics on my mind and tonight I thought &#8220;Why not put them online?&#8221; so that&#8217;s exactly what I did. Below I have written down some interesting topics I like and want to write about in the forthcoming days/weeks.

Bootstraps (already working on this)
Compiling PHP (an idea by [...]]]></description>
			<content:encoded><![CDATA[<p>When I started this blog I already had some topics on my mind and tonight I thought &#8220;Why not put them online?&#8221; so that&#8217;s exactly what I did. Below I have written down some interesting topics I like and want to write about in the forthcoming days/weeks.</p>
<ol>
<li>Bootstraps (already working on this)</li>
<li>Compiling PHP (an idea by <a href="http://jspr.tndy.me/">Jasper</a>)</li>
<li>More &#8220;PHP, Hell or Heaven?&#8221;</li>
<li>Running multiple PHP scripts at once (and let them communicate!)</li>
<li>eZ Components (especially the Console Tools)</li>
<li>Text analysis with PHP</li>
</ol>
<p>(You may have noticed that I copied some from the <a href="http://www.behindtheview.com/2009/11/23/mai-ocean-a-php-bot-server/">Mai Ocean introduction post</a>.)</p>
<p>When I was writing this I also came up with some questions: What do <strong>you</strong> think about these topics? Which do you prefer and do you have anything to add? Especially for the series &#8220;PHP, Hell or Heaven?&#8221; I would like to know what you like about PHP and (maybe even more) what you hate. Thus for all people out there that hate (or dislike) PHP: This is your time to speak! <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/srPy9KS3QUA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2009/11/29/interesting-topics/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2009/11/29/interesting-topics/</feedburner:origLink></item>
		<item>
		<title>PHP, Hell or Heaven: Data types</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/5WbEzpwO6cQ/</link>
		<comments>http://www.behindtheview.com/2009/11/27/php-hell-or-heaven-data-types/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 22:55:17 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP, Hell or Heaven]]></category>
		<category><![CDATA[assertions]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[hell or heaven]]></category>
		<category><![CDATA[pec]]></category>
		<category><![CDATA[spl]]></category>
		<category><![CDATA[type juggling]]></category>
		<category><![CDATA[types]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=144</guid>
		<description><![CDATA[his is the first post of the new series &#8220;PHP, Hell or Heaven?&#8221;. Today I&#8217;m going to talk about data types in PHP and compare their usage with other languages.

In PHP there are multiple data types but the main difference between other languages (especially more low level languages such as C++) is that you can [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_146" class="wp-caption alignleft" style="width: 224px"><img src="http://www.behindtheview.com/wp-content/uploads/2009/11/hellorheaven-214x300.png" alt="Hell or Heaven?" title="hellorheaven" width="214" height="300" class="size-medium wp-image-146" /><p class="wp-caption-text">Hell or Heaven?</p></div>This is the first post of the new series &#8220;PHP, Hell or Heaven?&#8221;. Today I&#8217;m going to talk about data types in PHP and compare their usage with other languages.</p>
<p>
In PHP there are <a href="http://php.net/manual/en/language.types.php">multiple data types</a> but the main difference between other languages (especially more low level languages such as C++) is that you can assign any value to any variable. This is also called <em>type juggling</em>. Therefore in PHP the type of an variable is defined by its content and not by the developer or by the way it&#8217;s been set-up.<span id="more-144"></span> For example, you could do the following in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color:#800080;">3.0</span><span style="color: #339933;">;</span></pre></div></div>

<p>You should note that the type changes in the example above. So the type will change from an integer to a string and finally to a float. In other languages you have to define a type which can&#8217;t and will not change. Now to get back to the topic (is PHP hell or heaven?) we have to answer a very important question: <em>do we like this?</em></p>
<p>For beginners I think it&#8217;s great. You can start with learning basic skills such as <a href="http://www.php.net/manual/en/language.control-structures.php">control structures</a> and don&#8217;t have to worry about the types of variables. On the other hand more restrictive rules will (possibly) make you a better programmer. When you can&#8217;t juggle with types, you won&#8217;t. Anyway when you are getting better at PHP you probably think about what you assign to variables so it becomes a bit less important. For instance I use <a href="http://www.phpdoc.org/">phpDoc</a> for every large project I work on so I define types for variables like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Will output name and value.
 * @param string $name
 * @param int $value
 * @return void
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> test<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">': '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you see I wrote the variable types in my docblock. Nevertheless nothing is stopping me from using other types. I could just run <em>test(23, 1)</em>. A possible solution to this is using assertions (see also <a href="http://www.behindtheview.com/2009/11/24/assertions-protection-against-yourself/">Assertions, protection against yourself</a>). So, if we combine phpDoc with assertions we would get:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Will output name and value.
 * @param string $name
 * @param int $value
 * @return void
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> test<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'is_string($name) &amp;&amp; is_int($value)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">': '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Above we have a function that only accepts variables of a defined type. That is good, if we accidentally used wrong values we will get an error. That is also very useful for people who don&#8217;t know how your code works but (have to) use it. Then again if we look at another language (in this example C++) we see how it can be done much and much better (and also easier):</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> test<span style="color: #008000;">&#40;</span>string name, <span style="color: #0000ff;">int</span> value<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span> name <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> value<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Combining all these facts I think PHP fails in <em>this</em> case. I like being strict and better type handling would improve that. We will also be relieved from writing all kinds of value checks which are boring..</p>
<p><strong>Is there a solution?</strong> Yes and no. Since PHP5 you can use type hinting for arrays and objects. In the header of the function you can define for only these two types what the value of the parameter must be:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> example1<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$param1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> example2<span style="color: #009900;">&#40;</span>Class_Of_Some_Kind <span style="color: #000088;">$param1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above is a good start (I use it very often) but you cannot yet use it for integers, floats and other types. For those types we have to do something else. I just recently found an experimental extension that gives more opportunities for better type handling: <a href="http://nl.php.net/manual/en/book.spl-types.php">SPL Type Handling</a>. This extension offers five objects you can use for type handling:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$integer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SplInt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$float</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SplFloat<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$boolean</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SplBool<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SplString<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The fifth object is SplEnum and is abstract. You can only use this object if you extend it. For each of these objects applies: if you try to assign an invalid value an UnexpectedValueException will be thrown. I have not yet used these new objects in one of my projects but I am definitely going to check them out. I don't think they are useful for all variables but they could be a nice addition to models. You can find more information about the usage of these objects on the documentation page:</p>
<p><a href="http://php.net/manual/en/book.spl-types.php">http://php.net/manual/en/book.spl-types.php</a></p>
<p>As I said, I haven't yet used them so I can't say anything about performance. If they are slow or consume to much memory they are useless. When I'll know more I will post my findings here. For now I think I've told you enough about type handling in PHP. It is time for you to decide! What do you think? Do you like the way types are handled in PHP? Is type juggling useful? Or is it like hell? What do you think about this new extension? Let me know <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/5WbEzpwO6cQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2009/11/27/php-hell-or-heaven-data-types/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2009/11/27/php-hell-or-heaven-data-types/</feedburner:origLink></item>
		<item>
		<title>Unit testing in PHP</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/AXxvdeVV1YE/</link>
		<comments>http://www.behindtheview.com/2009/11/25/unit-testing-in-php/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 16:27:24 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=141</guid>
		<description><![CDATA[Everyone&#8217; says unit testing is important. If I quote Wikipedia you should under stand why:

In computer programming, unit testing is a software verification and validation method in which a programmer tests if individual units of source code are fit for use.

However, I haven&#8217;t done it a lot. Why? Honestly I do not know. I tested [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_142" class="wp-caption alignleft" style="width: 104px"><img src="http://www.behindtheview.com/wp-content/uploads/2009/11/phpunit-logo.gif" alt="Unit testing with PHPUnit" title="phpunit-logo" width="94" height="80" class="size-full wp-image-142" /><p class="wp-caption-text">Unit testing with PHPUnit</p></div>&#8216;Everyone&#8217; says <em>unit testing</em> is important. If I quote Wikipedia you should under stand why:</p>
<blockquote><p>
<em>In computer programming, unit testing is a software verification and validation method in which a programmer tests if individual units of source code are fit for use.</em></p></blockquote>
<p><span id="more-141"></span><br />
However, I haven&#8217;t done it a lot. Why? Honestly I do not know. I tested PHPUnit last year but I never used it again. Anyway this is the time to change that! I&#8217;ve found a interesting series written by <a href="http://giorgiosironi.blogspot.com">Giorgio Sironi</a> titled: <em>Practical testing in PHP</em>.</p>
<p>I am definitely going to check it out. The articles can be found here:</p>
<li>Practical Testing in PHP (Serie)
<ul>
<li><a href="http://giorgiosironi.blogspot.com/2009/09/practical-testing-in-php-part-1-phpunit.html">part 1: PHPUnit usage</a></li>
<li><a href="http://giorgiosironi.blogspot.com/2009/09/practical-testing-in-php-part-2-write.html">part 2: write clever tests</a></li>
<li><a href="http://giorgiosironi.blogspot.com/2009/09/practical-testing-in-php-part-3.html">part 3: assertions</a></li>
<li><a href="http://giorgiosironi.blogspot.com/2009/09/practical-testing-in-php-part-4.html">part 4: fixtures</a></li>
<li><a href="http://giorgiosironi.blogspot.com/2009/09/practical-testing-in-php-part-5.html">part 5: annotations</a></li>
<li><a href="http://giorgiosironi.blogspot.com/2009/09/practical-testing-in-php-part-6.html">part 6: refactoring and patterns</a></li>
<li><a href="http://giorgiosironi.blogspot.com/2009/09/practical-testing-in-php-part-7-stubs.html">part 7: stubs</a></li>
<li><a href="http://giorgiosironi.blogspot.com/2009/10/practical-testing-in-php-part-8-mocks.html">part 8: mocks</a></li>
<li><a href="http://giorgiosironi.blogspot.com/2009/10/practical-testing-in-php-part-9-command.html">part 9: command line options</a></li>
</ul>
</li>
<p><em>Edit (26-11-2009)</em>:</p>
<p>I was reading some of the blogs that I follow and I also found a series about unit testing written by Jani Hartikainen. I have been following Jani for a while and he is a great inspiration so I recommend you to read these articles too. Anyway here they are:</p>
<ol>
<li><a href="http://codeutopia.net/blog/2009/06/05/unit-testing-introduction/">Unit testing introduction</a></li>
<li><a href="http://codeutopia.net/blog/2009/06/10/unit-testing-part-2-writing-and-running-tests/">Writing and running tests</a></li>
<li><a href="http://codeutopia.net/blog/2009/06/17/unit-testing-3-writing-tests-for-existing-code/">Writing tests for exsisting code</a></li>
<li><a href="http://codeutopia.net/blog/2009/06/26/unit-testing-4-mock-objects-and-testing-code-which-uses-the-database/">Mock Objects and testing code which uses the database</a></li>
<li><a href="http://codeutopia.net/blog/2009/07/04/unit-testing-5-test-driven-development/">Test-driven development</a></li>
</ol>
<p><em>Edit (03-12-2009:</em><br />
Giorgio Sironi (the author of the first series) released a e-book about unit testing. It contains his articles about unit testing and some new content:<br />
<a href=" http://giorgiosironi.blogspot.com/2009/12/practical-php-testing-is-here.html"><br />
http://giorgiosironi.blogspot.com/2009/12/practical-php-testing-is-here.html</a></p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/AXxvdeVV1YE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2009/11/25/unit-testing-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2009/11/25/unit-testing-in-php/</feedburner:origLink></item>
		<item>
		<title>Assertions, protection against yourself</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/P3pWoRK_oKw/</link>
		<comments>http://www.behindtheview.com/2009/11/24/assertions-protection-against-yourself/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 19:02:15 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[assert]]></category>
		<category><![CDATA[assertions]]></category>
		<category><![CDATA[assert_options]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[validating]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=111</guid>
		<description><![CDATA[When I write new code (especially objects) I tend to build in extra security measures to protect the code against myself or other developers. Please note that I do not mean users but only the people who use the code. To illustrate this we will look at the following example:

1
2
3
4
function calculate&#40;$number1, $number2&#41;
&#123;
    [...]]]></description>
			<content:encoded><![CDATA[<p><span class="dropCap">W</span>hen I write new code (especially objects) I tend to build in extra security measures to protect the code against myself or other developers. Please note that I do not mean users but only the people who use the code.<span id="more-111"></span> To illustrate this we will look at the following example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$number2</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$number1</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$number2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Of course this example is very simple, I also hope you never create a function for such a basic calculation. Nevertheless it will be useful as an example. If we put our function to the test we can see some problems:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'b'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Normally on a live server (with error reporting off) the output will be: 3, 1, 0 and 2. The first calculation is valid and just prints what we expected. The other calculations contain other characters than numbers so the outcome is a bit odd. Only the last calculation (when showing notices) will trigger a notice telling you that ArrayObject cannot be converted to a int. Of course we shouldn&#8217;t use non-numeric values with this function but during development we could made this kind of mistake.</p>
<p><strong>So how do we fix this?</strong> We need a way to tell us that we did something wrong. The most common way is to add some more code that checks our preconditions ($number1 and $number2 must be numeric). You could do this like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$number2</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'$number1 and $number2 must be numeric!'</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$number1</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$number2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This function will show a nice error message when you used wrong values. Using this method will make debugging easier because your code will tell you what went wrong. However why should we add five extra lines to our (one-line!) function? If we just remember the usage of this function we could also make sure we never use the wrong values as parameters. But then, if we forgot the usage we could spend our valuable developing time debugging why <em>calculate(&#8217;a', &#8216;b&#8217;);</em> went wrong.</p>
<p>So we have to find a good balance between less code (read less writing) and good usability. And this is the point <em>assertions</em> are useful. Assertions are pretty easy to use and you basically only have to know one function: <em>assert(mixed $assertion);</em>. As an parameter you use the assertion (or condition) you want to check. An example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abc'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'is_numeric($number)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When executing this code (through command line) we see the following result:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;</span> php <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$number</span> = 'abc'; assert('is_numeric(<span style="color: #007800;">$number</span>)');&quot;</span>
&nbsp;
Warning: assert<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>: Assertion <span style="color: #ff0000;">&quot;is_numeric(<span style="color: #007800;">$number</span>)&quot;</span> failed <span style="color: #000000; font-weight: bold;">in</span> Command line code on line <span style="color: #000000;">1</span></pre></div></div>

<p>Our assertion failed! And that is precisely what we wanted. Using this method we can easily check our preconditions on functions. The assertion inside the function will be checked and it will show an error when it returns false. So to improve our <em>calculate()</em> function we could do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$number2</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'is_numeric($number1) || is_numeric($number2)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$number1</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$number2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now we rewrote our check method in just one line. But there is more! We can also tweak the working of the assert function through <em>assert_options()</em>. There are five options you can change with this function but the most useful option (in my opinion) is ASSERT_BAIL. When enabled this option will make assertions terminate your application when failed. I find this very useful for testing. To enable ASSERT_BAIL just add the following line before your assertions:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">assert_options</span><span style="color: #009900;">&#40;</span>ASSERT_BAIL<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The other options and what they do can be found at the <a href="http://www.php.net/manual/en/function.assert-options.php">PHP documentation page</a>.</p>
<p>So far we have seen that assertions can be really useful yet <strong>should assertions be used for every check?</strong> In my opinion you should only use assertions to check parameters that are not user dependent and that can only be invalid during development. Assertions are a way to help you develop and debug your application, not short cuts for validating user input. Quoting from PHP.net:</p>
<blockquote cite="http://www.php.net/manual/en/function.assert.php"><p><em>&#8220;Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be TRUE and that indicate some programming errors if not or to check for the presence of certain features like extension functions or certain system limits and features.</p>
<p>Assertions should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated.&#8221;</em></p></blockquote>
<p>Hopefully you now understand the basic idea of assertions. They are great development tools and can even be disabled for production.</p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/P3pWoRK_oKw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2009/11/24/assertions-protection-against-yourself/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2009/11/24/assertions-protection-against-yourself/</feedburner:origLink></item>
		<item>
		<title>Mai Ocean, a PHP bot server</title>
		<link>http://feedproxy.google.com/~r/BehindTheView/~3/expBJR6UfXw/</link>
		<comments>http://www.behindtheview.com/2009/11/23/mai-ocean-a-php-bot-server/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 21:41:47 +0000</pubDate>
		<dc:creator>Wouter Bulten</dc:creator>
				<category><![CDATA[Mai]]></category>
		<category><![CDATA[Mai Ocean]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[jabber]]></category>
		<category><![CDATA[msn]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.behindtheview.com/?p=103</guid>
		<description><![CDATA[Besides studying and living I like to work on my Open Source project Mai. Mai is a project that consists of several sub projects and one in particular, Mai Ocean, I want to discuss here. It is currently my focus of development because it is an vital part of the Mai Project.
So what is Mai [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_139" class="wp-caption alignleft" style="width: 202px"><a href="http://launchpad.net/Mai-Ocean"><img class="size-full wp-image-139" title="mai-ocean-192" src="http://www.behindtheview.com/wp-content/uploads/2009/11/mai-ocean-192.png" alt="Mai Ocean Logo" width="192" height="192" /></a><p class="wp-caption-text">Mai Ocean Logo</p></div>
<p>Besides studying and living I like to work on my Open Source project <a href="http://www.mai-ai.org">Mai</a>. Mai is a project that consists of several sub projects and one in particular, <a href="http://launchpad.net/mai-ocean">Mai Ocean</a>, I want to discuss here. It is currently my <em>focus of development</em> because it is an vital part of the Mai Project.</p>
<p>So what is Mai Ocean exactly? In a few words you could say that it is a bot server written in PHP. Of course this doesn&#8217;t say much so let me explain further.<span id="more-103"></span> Mai Ocean can (or will be able to) manage an instant messenger bot that can interact using the MSN, Jabber and IRC protocol. Currently only the first is being developed.</p>
<p>A few weeks ago I finished the first draft version and now I decided to rewrite the code and eventually release it through <a href="http://www.launchpad.net">Launchpad</a>. In this process I will touch many PHP and coding related subjects and for that a great inspiration to write blog posts. Here is a small list that I want to discuss:</p>
<ol>
<li>Creating a good bootstrap and base structure for the application</li>
<li>Namespaces in use</li>
<li>eZ Components (especially the Console Tools)</li>
<li>Running multiple PHP scripts at once (and let them communicate!)</li>
<li>Designing a handler system</li>
</ol>
<p>Of course I will keep the topics as general as possible. I don&#8217;t except everyone to join my project and start helping me. (If you want, you&#8217;re welcome <img src='http://www.behindtheview.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ) If you want to check out the progress or get some more info you can go to the <a href="http://launchpad.net/Mai-Ocean">Mai Ocean project page</a>.</p>
<img src="http://feeds.feedburner.com/~r/BehindTheView/~4/expBJR6UfXw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.behindtheview.com/2009/11/23/mai-ocean-a-php-bot-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.behindtheview.com/2009/11/23/mai-ocean-a-php-bot-server/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 130.323 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-08-09 18:11:10 -->
