<?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>Juozas devBlog &#187; PHP</title>
	<atom:link href="http://dev.juokaz.com/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend Framework tips and tricks</title>
		<link>http://dev.juokaz.com/php/zend-framework-tips-and-tricks</link>
		<comments>http://dev.juokaz.com/php/zend-framework-tips-and-tricks#comments</comments>
		<pubDate>Fri, 29 Jan 2010 19:38:45 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[globals]]></category>
		<category><![CDATA[include path]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[request]]></category>
		<category><![CDATA[response]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[testability]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[views]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=964</guid>
		<description><![CDATA[ It&#8217;s always good to use great tools, but you need to make sure that you use them correctly, not just trying to code &#8220;just for it to work&#8221;. For this reason I decided to write down my usual list of things I mention when taking over some legacy project or just consulting someone how [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-460" title="Zend Framework" src="http://dev.juokaz.com/wp-content/uploads/2009/04/logo-zend-framework.jpg" alt="Zend Framework" width="169" height="114" /> It&#8217;s always good to use great tools, but you need to make sure that you use them correctly, not just trying to code &#8220;just for it to work&#8221;. For this reason I decided to write down my usual list of things I mention when taking over some legacy project or just consulting someone how to start.</p>
<p>Most of the outlined problems and solutions are focused on testability, maintainability and other good code practices. If you are not familiar with them, I recommend read about them ASAP as there is big chance that you are doing those things described in this post and don&#8217;t even realize how wrong they are. Believe me, you will soon find yourself a way better developer.</p>
<h5>Separate logic</h5>
<p>This one is the most obvious one, but trust me, I have found cases of it in every single project I have worked before (more than 10 with Zend Framework in a past half year and counting). If it&#8217;s controller, don&#8217;t do business logic, if it&#8217;s model don&#8217;t base behavior on POST parameters etc. Same applies to forms, bootstrap, views, various helpers and many more other components &#8211; logic should be separate and have its place. </p>
<p>Move all logic from controller to a model or <a href="http://dev.juokaz.com/programming/service-layer-in-web-applications">service</a>. Use forms only to handle validation and filtering, not to actually process data and persist it in any fashion. Hide session and authentication handling in one place and provide API for other algorithms. I can go on, but I hope it&#8217;s starting to get pretty clear, or at least it will eventually when you start testing your code: when you need to setup frontController, request, cookie and mail server just to test a form you realize that something is really wrong.</p>
<h5>Globals</h5>
<p>If you haven&#8217;t seen <a href="http://www.youtube.com/watch?v=-FRm3VPhseI">this</a> video, please do it immediately &#8211; you won&#8217;t regret it. Global state makes testing problematic and is completely opposite to what OOP proposes. This applies for use of <em>$_SERVER, $_SESSION</em> etc. values and all of them are accessible via request object (look at <em>Zend_Controller_Request_Http</em>) methods or separate classes, like Zend_Session. </p>
<p>Yet again I&#8217;m going to mention testability, because that&#8217;s something you always need to keep in mind. Test should not modify global variables, but rather inject mocked request/other objects which just return expected values (like client IP), because Zend Framework does pretty good job abstracting access to all global variables. </p>
<h5>Use form values, not request</h5>
<p>This one is both security and ease of coding issue, let&#8217;s look at this code sample:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$model</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$model</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">populate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>After form is validated (and hence values are filtered), raw values from request are still used from <em>$this->_request->getPost()</em>. Not only do you lose <em>Zend_Form</em> functions like ignored elements (for submit buttons for example) but also none of the filters are applied (which you use, yes?). Also I can pass pretty much anything I want and model needs to do way more validation. Hence <em>$form->getValues()</em> should always be used as it returns form data with respect to all defined rules and filters.</p>
<p>Form <em>populate()</em> method is also very overused. This method is designed to set default values for a form, from for example GB entry (when editing something) apart from that, you simply don&#8217;t need to use it as method <em>isValid()</em> sets values for all elements, so there is no need to apply default values too.</p>
<h5>Do not rely or use exit()/die()</h5>
<p>One of the first things I do is remove all these <em>exit()</em> calls and handle such cases with exceptions or <em>return</em> statements. There is only very very limited amount of situations where you actually need to use one of those functions. Just for example, imagine this controller action code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">userHasPermissions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form_Add<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">//submit form)</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// save with $form-&gt;getValues();</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// do something else</span></pre></div></div>

<p>First problem &#8211; thinking that <em>$this->_redirect()</em> will call <em>exit()</em> and hence nothing else will be executed. Even though this is true by default, this should be avoided in all cases. Not only it makes all <em>post*</em> events to not be fired, but also it makes testing impossible or incorrect. <em>Zend_Test</em> disables use of <em>exit()</em> in controller helpers, so in this case while testing you cannot test permissions checking as in all cases it will still execute later code. To fix this just add <em>return</em> in front of redirect (<em>return $this->_redirect(&#8216;/&#8217;)</em>) and you are safe.</p>
<p>Furthermore, second <em>exit()</em> is completely useless and makes code even more untestable. Again you can just use <em>return</em> to cancel later code (view will not be rendered as <em>viewRenderer</em> helper checks for redirection header and does nothing if detects such). From my experience, after saving entry, there is only some assignments to view in left code so you won&#8217;t suffer a lot if you just leave redirect, of course without <em>exit()</em>.</p>
<h5>Use a framework, not PHP</h5>
<p>This can sound wrong at first, but if you use a framework (Zend Framework in this case) don&#8217;t start throwing in hacks from 5 year old PHP apps. Like this (controller action):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Some_Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generateImage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">header</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: image/jpeg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$image</span><span style="color: #339933;">;</span></pre></div></div>

<p>I don&#8217;t even know where to start&#8230; All this logic is incorporated in response object, so you can do things like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image/jpeg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setBody</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It might seem as same thing, but it&#8217;s not. Yet again you can actually test it, you are not working with global state (<em>header()</em> is global state function) and request dispatch process is left working as it should. Controllers do not output any data (hence no echo should be used) they just get request object and return response object, that&#8217;s it. In a similar fashion as <em>exit()</em> breaks dispatch process, outputting from controller does that too, so don&#8217;t forget to make sure that you preserve <a href="http://dev.juokaz.com/wp-content/uploads/2009/12/sequence_globale.jpg">this flow</a>.</p>
<h5>Some small ones</h5>
<p><em>Application.ini</em> has a property <em>includePaths</em>, which is used to add additional paths to include path. Even though it works great, I still recommend not to use it because it will add those paths every time you create new application instance (true for 1.9, can change in future). If you try to do some controllers testing, you are probably going to instantiate it before every single test and after some hundreds of tests you will notice that somehow it&#8217;s getting slower and slower. That took me a few hours to find, though fix was easy, but still keep that in mind.</p>
<p>If you are using <a href="http://jquery.org">jQuery</a> or any other javascript view helpers, take full power of them. By that I mean use functions like <em>addJavascriptFile()</em>, <em>addJavascript()</em>, <em>addStylesheet()</em>, <em>addOnload()</em> etc. to add some additional resources and code from views. If you add javascript straight to view everything will still work, but by using view helper container you will have all your code nicely placed in one place and not scattered all other the place. </p>
<h5>Conclusion</h5>
<p>This is just a small list of problems and issue I have seen in my work &#8211; there are way more to look at (you can share some tips too). I hope those will give you some idea how to work with Zend Framework in a clean fashion and you will soon find that your code is starting to look nicer and coding time is decreasing as everything is nicely separated and transparent to other code. </p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/zend-framework-tips-and-tricks/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Why Zend Framework?</title>
		<link>http://dev.juokaz.com/php/why-zend-framework</link>
		<comments>http://dev.juokaz.com/php/why-zend-framework#comments</comments>
		<pubDate>Mon, 07 Dec 2009 21:01:18 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cla]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[consulting]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[scaffolding]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=937</guid>
		<description><![CDATA[ As you might have noticed I mostly write about Zend Framework and related content. This has a lot of underlying reasons, so I decided to wrap up my thoughts about it and why to (not)use Zend Framework for your own projects. This is not a comparison of frameworks though, because I don&#8217;t feel like [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-460" title="Zend Framework" src="http://dev.juokaz.com/wp-content/uploads/2009/04/logo-zend-framework.jpg" alt="Zend Framework" width="169" height="114" /> As you might have noticed I mostly write about <a href="http://framework.zend.com/">Zend Framework</a> and related content. This has a lot of underlying reasons, so I decided to wrap up my thoughts about it and why to (not)use Zend Framework for your own projects. This is not a comparison of frameworks though, because I don&#8217;t feel like having enough experiences with other frameworks to make a fare comparison, that&#8217;s why this is going to be only a Zend Framework analysis. </p>
<h5>History</h5>
<p>First time I have actually built an application with <a href="http://dev.juokaz.com/tag/zend-framework">Zend Framework</a> was about a half year ago, if I remember correctly version 1.8 was already around. Although my first applications were more than two years ago, but hey were more than tests and playing with at that time new features. So as I said above, not that long ago I switched most of my company&#8217;s code to use Zend Framework, contributed some code and have also started working as Zend Framework consultant (drop me an email if interested), so I think I have a pretty decent experience to judge it.</p>
<p>Before diving in to the analysis, I would like to clarify, that the reasons why I&#8217;m not using other frameworks are mainly caused by the fact that I didn&#8217;t had a chance to do that. Since I&#8217;m usually working with projects lasting more than 3 months, I need to be confident enough to know that after 2 months I don&#8217;t need to throw everything away and start from scratch. Also to test a framework for me is not to create a <a href="http://en.wikipedia.org/wiki/Hello_world_program">Hello World application</a>, I need to build something serious to know what features are missing and what problems can be a big issue. Nevertheless, I&#8217;m planning to do some research sometime.</p>
<h5>Advantages</h5>
<p>For me the most important piece of software: how <strong>customizable</strong> it is. As you might have noticed from the posts like <a href="dev.juokaz.com/programming/service-layer-in-web-applications">this</a>, I usually work with complex web application and would like to have ability to built it from ground up. Zend Framework was perfect for this, because everything is just a collection of building blocks, literally (or <a href="http://en.wikipedia.org/wiki/Loose_coupling">loosely coupled</a> in proper terms). I really liked that there is no <em>default</em> or even worse required structure and everything can be configured very easily.</p>
<p>Because I&#8217;m also in a position responsible for a whole project, <strong>quality</strong> for me is very important. I don&#8217;t know how other frameworks handle this, but <a href="http://www.zend.com">Zend</a> is pretty serious with what&#8217;s in framework and how the code is maintained. Starting from requirement to sign Contributor License agreement (known as <a href="http://framework.zend.com/wiki/display/ZFPROP/Contributor+License+Agreement">CLA</a>) and requirement of 80% unit-tests coverage, ending with <a href="http://devzone.zend.com/article/10049">bug hunt days</a>. Somehow it helps me to have a confidence to rely on the framework, and backwards compatibility has never been an issue (it&#8217;s <a href="http://devzone.zend.com/article/11364-DevZone-updated-to-Zend-Framework-v1.9.5">surprisingly good</a>).</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/12/zend2.gif"><img class="alignnone size-medium wp-image-950" title="Zend Framework components" src="http://dev.juokaz.com/wp-content/uploads/2009/12/zend2-300x219.gif" alt="Zend Framework components" width="210" height="153" /></a><strong>Variety of components</strong> and libraries makes development faster because I do not need to search for another library (there is also coding standards problem and a lot of smaller libraries are just a bunch of files, without any clear structure). As of today, I probably have used more than 90% of available components and because they tend to share the same configuration style or just work style, it didn&#8217;t took long to start using them. For example, just last night I converted whole application to use custom routes (one custom route per action) in about 4 hours, without having done it before at all.</p>
<p><strong>Community</strong> is number one best thing of Zend Framework. Apart from contributors, there are thousands of people willing to help and share their knowledge. Usually I hangout in twitter (<a href="http://www.twitter.com/juokaz">here</a>), but there are also IRC channels, mailing lists, forums and many more. I&#8217;m subscribed to hundreds of PHP blogs and from what I can see, Zend Framework articles are most common and usually in a very good quality. For me web applications development is not just writing code, so having a wonderful community really helps.</p>
<h5>Disadvantages</h5>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/12/sequence_globale.jpg"><img class="alignnone size-medium wp-image-943" style="float: right; margin-left: 5px;" title="Zend Framework dispatch" src="http://dev.juokaz.com/wp-content/uploads/2009/12/sequence_globale-300x201.jpg" alt="Zend Framework dispatch" width="210" height="141" /></a><strong>Hard to learn</strong>. This is not a problem (any more) for me, but can be tricky when working with others, especially with things like form decorators. Even though Zend Framework has a very good <a href="http://framework.zend.com/manual/en/zend.form.elements.html#zend.form.elements.decorators">documentation</a>, I would say, that used techniques are sometimes not straightforward for starters and can lead to a very bad code. As with example with form decorators, once you <em>get</em> them, work with forms will be a joy, however at first they seem as pointless complication. I guess the best thing is just read the manual properly (<a href="http://en.wikipedia.org/wiki/RTFM">RTFM</a> in more rude words) &#8211; you will benefit later.</p>
<p>For some an issue can be that Zend Framework has <strong>no <a href="http://en.wikipedia.org/wiki/Scaffold_(programming)">scaffolding</a></strong>. In short, scaffolding is a user interface and logic generation based on database description and business logic definitions. If you have used frameworks like <a href="http://www.djangoproject.com/">Django</a> (my favorite for Python, has perfect <a href="http://docs.djangoproject.com/en/dev/intro/tutorial02/">scaffolding</a>), you will miss these things and it can be a first &#8220;brick wall&#8221; for your learning. Zend Framework has <a href="http://framework.zend.com/manual/en/zend.tool.project.html">Zend_Tool</a> component which provides basic project generation (you can customize it easily), but this is pretty much everything.</p>
<p>Connected to the both issues outlined above, Zend Framework in general is sometimes <strong>too much loosely coupled</strong>. Because all the components are just separate classes and out of the box you don&#8217;t get any sort of web application, one might have problems trying to understand what to do with all this <em>stuff</em> at all. It&#8217;s a great feature for me, but for someone not having any experience with Zend Framework it is probably impossible to build application without a help of blogs, books or (my recommendation) manual.</p>
<p>As you might have guessed, the fact that Zend Framework has lots of components also means that there should be enough developers to support them. However, this is not always the case and some components are really <strong>lacking new features or updates</strong>. For example <a href="http://framework.zend.com/manual/en/zend.pdf.html">Zend_Pdf</a> is quite out-dated and not moving as fast as a lot of us would like to. But hey, it&#8217;s an open-source framework, so if you have ideas of how to improve things &#8211; you are free to do, believe <a href="http://framework.zend.com/wiki/display/ZFPROP/Zend_Db_Adapter_Sqlsrv+-+Juozas+Kaziukenas+and+Rob+Allen">me</a>, it&#8217;s a great fun to contribute to such a influential software application. Nevertheless, always evaluate available components and choose what&#8217;s best for your project.</p>
<p>There is also the funny part &#8211; misconceptions and myths. I&#8217;ve just started working on a new article about these, so expect some interesting things very soon.</p>
<h5>Conclusion</h5>
<p>I hope it&#8217;s clear, that most of the advantages and disadvantages are coming from the same root &#8211; depending on what experience you have and what you are looking for Zend Framework can be a good or absolutely awful choice. For me it&#8217;s a perfect tool and currently I have no plans (even though <a href="http://www.symfony-project.org/">Symfony</a> is in my todo list) to use something else, and if you look at things which are coming in version 2.0 of both Doctrine and Zend Framework (yes, they will have integration)&#8230; Simply a good solution for serious work.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/why-zend-framework/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Zend Framework and Doctrine. Part 3</title>
		<link>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-3</link>
		<comments>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-3#comments</comments>
		<pubDate>Mon, 30 Nov 2009 11:57:53 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=909</guid>
		<description><![CDATA[ During last two months I spent massive amount of time tweaking Doctrine ORM framework and making it to perform as fast as possible (as you might have noticed from my never ending tweets). This post is devoted to performance and efficiency, with practical tips &#038; tricks how to reduce memory usage, make it work [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.juokaz.com/wp-content/uploads/2009/11/doctrine-orm-php5.png" alt="doctrine-orm-php5" title="doctrine-orm-php5" width="191" height="53" class="alignnone size-full wp-image-847" style="margin-right: 5px;" /> During last two months I spent massive amount of time tweaking Doctrine ORM framework and making it to perform as fast as possible (as you might have noticed from my never ending <a href="http://twitter.com/juokaz">tweets</a>). This post is devoted to performance and efficiency, with practical tips &#038; tricks how to reduce memory usage, make it work faster and save resources.</p>
<p>Doctrine is a very powerful framework, however you should study its behavior a little bit to get it working properly. As it turns out &#8211; it&#8217;s not that hard. </p>
<h5>Speed</h5>
<p>One of the first things I recommend looking at is <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/caching:query-cache-&#038;-result-cache">query cache</a>. Because there is quite a lot happening in turning <a href="http://www.doctrine-project.org/documentation/manual/1_2/nl/dql-doctrine-query-language">DQL</a> statement into the SQL query, <strong>caching</strong> that process can increase performance by a big margin. Best choice &#8211; <a href="http://en.wikipedia.org/wiki/PHP_accelerator">APC</a>, although robo47.net has an <a href="http://www.robo47.net/codeschnipsel/32-Adapter-fuer-Doctrine_Cache-zu-Zend_Cache">example</a> code for use of Zend_Cache adapters, just don&#8217;t forget that file back-end is probably not the best pick.</p>
<p><strong>Hydrators</strong> are probably the easiest thing to misuse. Hydration in Doctrine language is turning SQL query results into data graph. It can be an array, object or single value. It&#8217;s very nice to get results as PHP objects (in this case &#8211; models), however it&#8217;s slow. Slower than hydrating like array, and much slower than getting raw result from <a href="http://uk2.php.net/manual/en/book.pdo.php">PDO</a>. That&#8217;s why I always recommend answering one simple question: &#8220;will you be updating/deleting records?&#8221;. If the answer is no &#8211; hydrate as array, because you are not going to need an actual model (usually).</p>
<p>For this reason I always try to use array notation to access properties rather than like-object-vars one. Basically instead of <em>$product->name</em> I tend to write <em>$product['name']</em>, which returns the same result, but makes <strong>switching to array hydration very easy</strong>. You can find more tips and tricks at Doctrine manual <a href="http://www.doctrine-project.org/documentation/manual/1_2/en/data-hydrators">here</a>, but the key moment is to remember that the faster data structure in PHP is array (I believe so), so if application is not performing well &#8211; start playing with hydrations first.</p>
<p>Doctrine has a very nice support for relations, where they work like proxies and data can be retrieved when it&#8217;s needed (lazy-loading). However, this is wrong:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Comments</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">print</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">NewsItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This code is bad, because for each <em>comment</em> you will load <em>news item</em> using a <strong>separate query</strong>, hence you are wasting db server resources and making code slower. Optimization is pretty straightforward:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> Doctrine_Query<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comments C'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">innerJoin</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'C.NewsItem N'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'C.user_id = ?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">print</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">NewsItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here all the data is loaded in <strong>one query</strong> and everything happens much faster (and in this case making it to hydrate as array would also improve the performance).</p>
<p>However, make sure you <strong>know what you are joining</strong>. For example imagine that in previous example <em>news item</em> is also one-to-many (comment has many news items) relation and user <em>1</em> has written 1000 comments where each comment is attached to average <em>news items</em>. Query above will return 50&#8242;000 records (<a href="http://en.wikipedia.org/wiki/Cross_product">50 * 1000</a>) which Doctrine will need to hydrate then. This will be very slow and probably going to kill your web server after some time. One day I had a query which was returning 2GB of data, server admins where probably not very happy about it&#8230;</p>
<h5>Memory</h5>
<p>One of my favorite parts of software development is playing with memory usage and making it efficient. Even though it sounds really simple, debugging it and finding it where the memory is <a href="http://en.wikipedia.org/wiki/Memory_leak">leaking</a> is not an easy task. Recently I was using Doctrine to work with quite big datasets (on average 50&#8242;000 of records) and probably have tried all the possible tricks to make Doctrine memory efficient. My code looked really simple:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> Doctrine_Query<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// do some work here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You might expect that memory usage would be steady, however it is not. Doctrine uses <a href="http://martinfowler.com/eaaCatalog/identityMap.html">identity map</a> and objects also have a lot of references which makes <strong>freeing up memory</strong> a tricky job. As my experience showed, even though records have a method <em>free()</em> which is supposed to de-reference it, sometimes it doesn&#8217;t help.</p>
<p>So to make memory management work, make sure to try these:</p>
<ul>
<li><strong>$record->free(true)</strong> &#8211; deep free-up, calls <em>free()</em> on all relations too</li>
<li><strong>$collection->free()</strong> &#8211; free all collection references</li>
<li><strong>Doctrine_Manager::connection()->clean()</strong> &#8211; cleanup connection (and remove identity map entries)</li>
</ul>
<p>With some debugging and profiling (and these methods) you hopefully can make memory usage to be low. I&#8217;m also using some custom iterators (available <a href="http://github.com/juokaz/php-examples/tree/master/doctrine-iterators/">here</a>) to <strong>divide query into chunks</strong>, because loading 50&#8242;000 of objects in one go is not going to work, so you might want to look at it too. </p>
<p>Another recommended tip: make sure to <strong>free queries</strong> too. As collections hold references to all records, query object also has some references to parsed sub-parts of query. For this I use auto-free setting enabled by (available in my first part post also <a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-1">here</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// enable automatic queries resource freeing</span>
<span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>
	Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_AUTO_FREE_QUERY_OBJECTS</span><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>It&#8217;s very easy to forget to free a query and it&#8217;s again creating these references which makes <a href="http://php.net/manual/en/function.gc-enable.php">garbage collector</a>&#8217;s work hard. Nevertheless, after enabling this one I don&#8217;t know any other place where the memory can start to leak.</p>
<p>Last step &#8211; <strong>PHP 5.3</strong>. I&#8217;ve been working with this version for a few months and it works great, so if it&#8217;s possible &#8211; I recommend using it (you will also help with testing frameworks, but both Zend Framework and Doctrine already should work fine). One important function here is <a href="http://php.net/releases/5_3_0.php">improved</a> garbage collector, so the actual script takes less memory to execute. I haven&#8217;t recorded benchmarks on this one, but what I&#8217;ve noticed during development is that 5.3 does in fact has a lower peak memory usage. </p>
<h5>Conclusion</h5>
<p>I think I haven&#8217;t missed anything here, or at least these are the tips which made applications work fast (if I have &#8211; let me know). To finish with &#8211; good optimizations are done by comprehensive benchmarking and profiling, so the fact that Doctrine is a big framework doesn&#8217;t necessary mean it&#8217;s slow too. At the end of the day, it&#8217;s usually only a matter of reading a manual. </p>
<p><strong>All parts</strong>:</p>
<ol>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-1">Zend Framework and Doctrine. Part 1</a></li>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-2">Zend Framework and Doctrine. Part 2</a></li>
<li><a href="http://dev.juokaz.com/php/zend-framework-and-doctrine-part-3">Zend Framework and Doctrine. Part 3</a></li>
</ol>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/zend-framework-and-doctrine-part-3/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Finalizing WinPHP competition</title>
		<link>http://dev.juokaz.com/winphp-2009/finalizing-winphp-competition</link>
		<comments>http://dev.juokaz.com/winphp-2009/finalizing-winphp-competition#comments</comments>
		<pubDate>Mon, 01 Jun 2009 10:23:44 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[competition]]></category>
		<category><![CDATA[deepzoom]]></category>
		<category><![CDATA[jelly fish]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[rob allen]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[sql driver]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[winphp]]></category>
		<category><![CDATA[zfmssql]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=824</guid>
		<description><![CDATA[It&#8217;s been more than a month since this competition started, but the time ran very quickly. Today I&#8217;m going to summarize what I&#8217;ve used to create my entry and what I&#8217;ve learned.
Final application available at http://winphp.juokaz.com:82/.
Goal was:
My project will allow people to upload huge collections of photos (probably archived in one zip file) and get [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-830" style="margin-right: 5px; float: left;" title="Gallery" src="http://dev.juokaz.com/wp-content/uploads/2009/06/gallery.jpg" alt="Gallery" width="159" height="146" />It&#8217;s been more than a month since this competition started, but the time ran very quickly. Today I&#8217;m going to summarize what I&#8217;ve used to create my entry and what I&#8217;ve learned.</p>
<p>Final application available at <a href="http://winphp.juokaz.com:82/">http://winphp.juokaz.com:82/</a>.</p>
<p>Goal <a href="http://dev.juokaz.com/winphp-2009/first-annual-winphp-challenge">was</a>:</p>
<blockquote><p>My project will allow people to upload huge collections of photos (probably archived in one zip file) and get nice online gallery.</p></blockquote>
<p>I decided not to use archived files and simply allowed to upload multiple files using Silverlight <a href="http://www.codeplex.com/SilverlightFileUpld">control</a>, but pretty much everything left the same. My approach to this competition was to focus on technologies and not on functionality. That&#8217;s why I spent huge amount of time creating abstractions and making parts of application to be very customizable and not adding a lot of functions.</p>
<p>Functions in my app are really easy to add and in a matter of some lines one can add various parameters to images (tags, name etc.) and then sort/filter them &#8211; a lot of hard work is done under the hood. Silverlight based gallery is also absolutely independent from whole application and will work as long as front-end supplies correct gallery xml file, hence it can be easily customized. </p>
<p>Rules had 4 criterias:</p>
<ol>
<li> Application originality &#8211; I think I passed this one</li>
<li> Completeness &#8211; I have done everything what I&#8217;ve wanted</li>
<li> Use of Windows specific features/services &#8211; uses <a href="http://www.php.net/manual/en/book.com.php">COM</a> objects, Silverlight</li>
<li> Documentation of the process &#8211; <a href="http://dev.juokaz.com/category/winphp-2009">articles</a> in this blog and a lot of <a href="http://search.twitter.com/search?q=&#038;ands=&#038;phrase=&#038;ors=&#038;nots=&#038;tag=winphp&#038;lang=all&#038;from=juokaz&#038;to=&#038;ref=&#038;near=&#038;within=15&#038;units=mi&#038;since=&#038;until=&#038;rpp=15">tweets</a></li>
</ol>
<p>I think I have done everything that was required and now will try to push this application even further. Maybe Microsoft itself will show interest in it, because what I&#8217;ve done and used has been key topics in <a href="http://www.microsoft.com/events/mix/default.mspx">MIX 09</a>.</p>
<p><img class="alignnone size-full wp-image-826" title="Gallery x" src="http://dev.juokaz.com/wp-content/uploads/2009/06/gallery-x_1243856343122.png" alt="Gallery x" width="555" height="152" /></p>
<p><strong>Silverlight</strong> part and images processing is based on <a href="http://jellyfishdz.codeplex.com/">Jellyfish</a> library. At first I used <a href="http://dev.juokaz.com/winphp-2009/creating-deep-zoom-applications-with-c">Microsoft libraries</a>, but soon I got stuck because of lack of documentation and functionality. However Jellyfish is far from perfect &#8211; a lot of things are hardcoded, made <em>private</em> and hard to change. Also, it has some functions which are useful only in rare cases and need to be removed. For example, each mouse move, click and scroll used to look through all images in scene (using loop) and detect which one is under the mouse cursor. Very inefficient (especially if you don&#8217;t need it).</p>
<p><strong>Zend Framework</strong> was used to power whole website. I didn&#8217;t find any difference running it on Windows from running on Linux, so I can&#8217;t say much about <a href="http://dev.juokaz.com/winphp-2009/zend-framework-and-microsoft-iis">anything specific</a>. However, I <a href="http://dev.juokaz.com/winphp-2009/sql-native-client-as-mssql-driver-for-zend-framework">decided</a> to use the new <a href="http://sql2k5php.codeplex.com/">Sql driver</a> implementation for PHP, but it wasn&#8217;t included in Zend Framework supported adapters <a href="http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.adapter-notes">list</a>. That&#8217;s why me and <a href="http://akrabat.com/">Rob Allen</a> started a <a href="http://zfmssql.codeplex.com/">project</a> at <a href="http://www.codeplex.com">codeplex</a> to create an adapter for sql driver. It&#8217;s almost complete and at least both of us use it for our projects &#8211; everyone is free to test it and suggest changes though.</p>
<p><strong>Windows</strong> was good enough. I haven&#8217;t used Windows for any development for more than two years, but it worked surprisingly good. <a href="http://www.netbeans.org/">NetBeans</a> works the same in all platforms, but Visual Studio was almost a new thing. Nevertheless, both have done their job. <a href="http://www.iis.net/">IIS</a> was a <a href="http://dev.juokaz.com/winphp-2009/setting-up-windows-for-php-server">new thing</a> too, but I didn&#8217;t find a big difference for a developer, only that it allows to change everything without touching config files &#8211; I found it faster when settings are in multiple places.</p>
<p><strong>Community </strong>is outstanding. Stuart Herbert,  Rob Allen, Alton Crosslen and organizer Bram Veenhof were all chatting on <a href="http://search.twitter.com/search?q=winphp">Twitter</a> and it felt more like a community project and not a competition. Also the level of entries, from my point of view, is really high &#8211; this competition doesn&#8217;t had a lot of entrants (only slightly more than 10), but all of them are really competitive. </p>
<p>It was really a great time.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/finalizing-winphp-competition/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use external libraries in PHP?</title>
		<link>http://dev.juokaz.com/winphp-2009/how-to-use-outside-libraries-in-php</link>
		<comments>http://dev.juokaz.com/winphp-2009/how-to-use-outside-libraries-in-php#comments</comments>
		<pubDate>Sat, 23 May 2009 14:24:58 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[com]]></category>
		<category><![CDATA[competition]]></category>
		<category><![CDATA[deepzoom]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[gac]]></category>
		<category><![CDATA[gacutil]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[php extensions]]></category>
		<category><![CDATA[regasm]]></category>
		<category><![CDATA[winphp]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=793</guid>
		<description><![CDATA[External libraries are useful for performance demanding tasks where PHP is simply too slow. Also PHP can work as front-end system for various back-end systems (where server doesn&#8217;t provide any PHP supported communication types). I have written some posts about using .Net libraries in PHP so far, but there are some other choices available too. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/osoft_1490922690php-logo-300x158.png" alt="osoft_1490922690php-logo" title="osoft_1490922690php-logo" width="150" height="79" class="alignnone size-medium wp-image-800" />External libraries are useful for performance demanding tasks where PHP is simply too slow. Also PHP can work as front-end system for various back-end systems (where server doesn&#8217;t provide any PHP supported communication types). I have written some posts about using <a href="http://dev.juokaz.com/winphp-2009/using-php-with-c-written-libraries">.Net libraries in PHP</a> so far, but there are some other choices available too. To start with, there are two main categories of possible external code usage in PHP:</p>
<ol>
<li><a href="http://pecl.php.net/">PHP extensions</a></li>
<li><a href="http://uk.php.net/manual/en/class.com.php">COM</a> objects, programs executed with exec()</li>
</ol>
<p>Today I&#8217;m going to look at all of them and explain my choice to use COM objects and not others from above. </p>
<h5>Three choices</h5>
<p><strong>PHP extensions</strong> (how to create them read <a href="http://devzone.zend.com/article/1021">here</a>) are fast and (at least should be) stable. However they are quite hard to create at start and uses <a href="http://en.wikipedia.org/wiki/C_programming_language">C</a>. For example, I&#8217;m now using library written in C# and rewriting it in C just to use it as extension is way to complicated and time consuming (and probably not worth it). I can only image PHP extensions as libraries to optimize some parts of code or to be used in multiple projects. In my <a href="http://dev.juokaz.com/winphp-2009/first-annual-winphp-challenge">case</a>, I only need to some limited tasks.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/visualstudio-6.png"><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/visualstudio-6-150x150.png" alt="visualstudio-6" title="visualstudio-6" width="100" height="100" style="float: right; margin-left: 5px;" class="alignnone size-thumbnail wp-image-805" /></a>Next logical choice would be executing programs with <strong>exec()</strong>. In my opinion it&#8217;s the easiest way because programs can be created with any language you like, can be supplied with source code and none of system settings need be changed. If exec() is not blocked in php.ini it&#8217;s very practical solution. </p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/expolorer-1.png"><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/expolorer-1-150x150.png" alt="expolorer-1" title="expolorer-1" width="100" height="100" style="float: right; margin-left: 5px;" class="alignnone size-thumbnail wp-image-807" /></a>Windows users has an option to use <strong>COM objects</strong>. COM objects stand somewhere between PHP extensions and exec(). They can be created in many languages, but libraries are not executed outside PHP script &#8211; they behave as normal PHP objects (almost). They are more convenient to work with than compiled programs, but need to registered with Windows.</p>
<h5>The best</h5>
<p>I was testing last two solutions for more than two weeks and my final choice is&#8230; COM objects.  To explain this choice here is a short story:</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/visualstudio-7.png"><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/visualstudio-7-150x150.png" alt="visualstudio-7" title="visualstudio-7" width="100" style="float: right; margin-left: 5px;" height="100" class="alignnone size-thumbnail wp-image-809" /></a>I have a webpage which allows uploading multiple pictures. Two files are uploaded in parallel and once uploaded they are processed (resized, thumbnail and special structure for <a href="http://dev.juokaz.com/winphp-2009/creating-deep-zoom-applications-with-c">DeepZoom</a> is created). All this processing is written in C# and uses various windows and 3rd-party libraries.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/firefox-1.png"><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/firefox-1-150x150.png" alt="firefox-1" title="firefox-1" width="100" height="100" style="float: right; margin-left: 5px;" class="alignnone size-thumbnail wp-image-803" /></a>At first I tried compiling a program as &#8220;console application&#8221; (in VisualStudio) and then run it with exec(). It worked as expected, but was a little bit too slow. Script was taking around 3 s. to execute, even though actual program ran in 1 s. I didn&#8217;t spent much time analyzing where overhead was coming from but it was clear that it can be optimized.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/iis-5.png"><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/iis-5-150x150.png" alt="iis-5" title="iis-5" width="100" height="100" style="float: right; margin-left: 5px;" class="alignnone size-thumbnail wp-image-796" /></a>So I compiled program as &#8220;class library&#8221;, registered it with the <a href="http://en.wikipedia.org/wiki/Global_Assembly_Cache">Windows assembly cache</a> and then used with a <a href="http://uk.php.net/manual/en/class.dotnet.php">DOTNET</a> class. First problem was IIS server &#8211; it <a href="http://twitter.com/juokaz/status/1880755692">kept</a> throwing 500 errors no matter what I did. I didn&#8217;t wanted to waste my time trying to find where was a problem &#8211; after changing to COM class it magically started to work (even though it uses the same library). Using my library inside PHP was much more faster and execution time reduced to less than a second (depends on a size of image).</p>
<h5>Conclusion</h5>
<p>I have tested most of possible ways to execute external code. Even though COM objects perform better they can&#8217;t be suggested as default choice, because libraries need to registered with a system what can be problematic and maybe not even possible. So if you have limited permissions to server &#8211; use exec(), but in all other cases &#8211; I definitely recommend using COM. PHP extensions are even better, but harder to code and limited to C.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/how-to-use-outside-libraries-in-php/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Passing data from PHP to Silverlight</title>
		<link>http://dev.juokaz.com/winphp-2009/passing-parameters-from-php-to-silverlight</link>
		<comments>http://dev.juokaz.com/winphp-2009/passing-parameters-from-php-to-silverlight#comments</comments>
		<pubDate>Fri, 15 May 2009 21:38:23 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[connect]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[params]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[winphp]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=745</guid>
		<description><![CDATA[Only on rare cases applications created with Silverlight (or Flash) are static &#8211; it&#8217;s very common to have information coming from a RSS feed, REST service or any other data source. What is more important, these applications usually needs configuration variables (user name, language, products category, etc.) to be passed to them. But how to [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.juokaz.com/wp-content/uploads/2009/04/microsoft_silverlight_c-269x300.jpg" alt="Silverlight" title="Silverlight" width="90" height="100" class="alignnone size-medium wp-image-457" />Only on rare cases applications created with <a href="http://en.wikipedia.org/wiki/Silverlight">Silverlight</a> (or Flash) are static &#8211; it&#8217;s very common to have information coming from a RSS feed, <a href="http://en.wikipedia.org/wiki/REST">REST</a> service or any other data source. What is more important, these applications usually needs configuration variables (user name, language, products category, etc.) to be passed to them. But how to do all that?</p>
<h5>Configuration syntax</h5>
<p>Flash uses <a href="http://www.permadi.com/tutorial/flashVars/index.html"><em>flashvars</em></a> <em>object</em> parameter which is easy to use and passed variables automatically become available within actionscript variables scope. Luckily, Silverlight has almost the same thing.</p>
<p>Flash: <em>name1=value1<strong>&#038;</strong>name2=value2<strong>&#038;</strong>name3=value3</em><br />
Silverlight: <em>name1=value1<strong>,</strong>name2=value2<strong>,</strong>name3=value3</em></p>
<p>To start with, lets create a simple script which will form an arguments string (you can use <em>array</em> of params and then implode with &#8216;,&#8217;, but for the sake of simplicity I just use static string):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$connect</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/users/juozas'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Connect=&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connect</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;,id=1,somethingElse=true&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Inside an <em>object</em> element in HTML add this code (depending on framework you (not)use you may need to change it to work as a template or a view script):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;param name=&quot;initParams&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$args</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;</pre></div></div>

<h5>Reading configuration in Silverlight</h5>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/silverlight-1.png"><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/silverlight-1-150x150.png" alt="silverlight-1" title="silverlight-1" style="float: right; margin-left: 5px;" width="100" height="95" class="alignnone size-thumbnail wp-image-762" /></a>If you start with a default Silverlight project in Visual Studio, you initially have two classes: App and Page. <em>App</em> is a main class, which (by default) on start up creates a new <em>Page</em> instance and assigns it to a root visual element of itself (<em>App</em>), where <em>Page</em> is your actual Silverlight application (visual part, controls, etc.). Standard start up event in <em>App</em> looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Application_Startup<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, StartupEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">RootVisual</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Page<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>To use parameters from HTML tag, we need to get <em>initParams</em> from the passed <strong>e</strong> argument (which has type of <a href="http://msdn.microsoft.com/en-us/library/system.windows.startupeventargs.aspx">StartupEventArgs</a>) and set corresponding properties of some object to their values. You can have global static configuration class, but (also for simplicity) I just pass them to the <em>Page</em> instance:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Application_Startup<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, StartupEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Page page <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Page<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">InitParams</span>.<span style="color: #0000FF;">Keys</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Connect&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> 
        <span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">InitParams</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Connect&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
     page.<span style="color: #0000FF;">connectPath</span> <span style="color: #008000;">=</span> HttpUtility.<span style="color: #0000FF;">UrlDecode</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">InitParams</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Connect&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">RootVisual</span> <span style="color: #008000;">=</span> page<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Here I&#8217;m setting <em>connectPath</em> using <em>Connect</em> from <em>initParams</em>. One last thing &#8211; don&#8217;t trust user input, if you have integers, use <em>Int32.Parse()</em> and always check if passed values are in range of expected values. It&#8217;s really important to remember that SQL injections and other similar attacks are also threat in Silverlight (as Flash also).</p>
<h5>Outside information sources</h5>
<p>However, not all properties can be passed initially &#8211; some times it&#8217;s required to refresh information from server during actual runtime (think <a href="http://en.wikipedia.org/wiki/Ajax_(programming)">AJAX</a>). You can easily create asynchronous calls to server from Silverlight too (works almost exactly the same as normal JavaScript AJAX script):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> Page<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    InitializeComponent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    LoadButton.<span style="color: #0000FF;">Click</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #000000;">&#40;</span>LoadButton_Click<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">void</span> LoadButton_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, RoutedEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    var wc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    wc.<span style="color: #0000FF;">OpenReadCompleted</span>
            <span style="color: #008000;">+=</span><span style="color: #008000;">new</span> OpenReadCompletedEventHandler<span style="color: #000000;">&#40;</span>wc_OpenReadCompleted<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    wc.<span style="color: #0000FF;">OpenReadAsync</span><span style="color: #000000;">&#40;</span> <span style="color: #008000;">new</span> Uri<span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;/script.php?id=1&quot;</span>, UriKind.<span style="color: #0000FF;">Relative</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    Text.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Loading...&quot;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">void</span> wc_OpenReadCompleted<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, OpenReadCompletedEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span> e.<span style="color: #0000FF;">Error</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Do something with an error</span>
        Text.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">Error</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">else</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">try</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// e.Result has a type of Stream, so we need to read it first</span>
            <span style="color: #008080; font-style: italic;">// (not just cast to string)</span>
            StreamReader rdr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Result</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Text.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> rdr.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">finally</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span> e.<span style="color: #0000FF;">Result</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #000000;">&#41;</span> e.<span style="color: #0000FF;">Result</span>.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/silverlight-2.png"><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/silverlight-2-150x150.png" alt="silverlight-2" title="silverlight-2" style="float: right; margin-left: 5px;" width="100" height="95" class="alignnone size-thumbnail wp-image-764" /></a>This code creates <em>onClick</em> event which asynchronously downloads &#8220;/script.php?id=1&#8243; and sets results to <em>TextBlock</em> element named &#8220;Text&#8221;. You can use absolute URLs too, but don&#8217;t forget that cross-domain requests protection will try to block it. Look <a href="http://timheuer.com/blog/archive/2008/04/06/silverlight-cross-domain-policy-file-snippet-intellisense.aspx">here</a> to read more about how to avoid it. Not even localhost will work if you are testing with VisualStudio (witch creates temporary &#8220;ASP.NET development server&#8221; with random port (not 80)), so make sure to check it first. </p>
<p>As you can see it&#8217;s very easy and simple to create dynamical Silverlight applications. I have done some work with Flash too (probably more than with Silverlight, especially with <a href="http://en.wikipedia.org/wiki/ActionScript">ActionScript language</a>) and I can confirm that there isn&#8217;t much difference. Silverlight is more like <a href="http://en.wikipedia.org/wiki/Adobe_Flex">Flex</a> though.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/passing-parameters-from-php-to-silverlight/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Native Client as MSSQL driver for Zend Framework</title>
		<link>http://dev.juokaz.com/winphp-2009/sql-native-client-as-mssql-driver-for-zend-framework</link>
		<comments>http://dev.juokaz.com/winphp-2009/sql-native-client-as-mssql-driver-for-zend-framework#comments</comments>
		<pubDate>Thu, 07 May 2009 01:39:53 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[competition]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[driver]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[odbc]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[sql native client]]></category>
		<category><![CDATA[sqlsrv]]></category>
		<category><![CDATA[wiinphp]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=648</guid>
		<description><![CDATA[Stuart Herbert in his blog more than a year ago pointed some key Microsoft Sql extension for PHP pros:
What’s Wrong With The Existing MSSQL Extension For PHP?
… or, why do we need an improved SQL Server extension for PHP? :)
The existing MSSQL extension works well, but has a few practical limitations that have to be [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/ms_sql_logo2.jpg" alt="Microsoft Sql server" title="Microsoft Sql server" width="163" height="56" class="alignnone size-full wp-image-672" /><a href="http://blog.stuartherbert.com/php/">Stuart Herbert</a> in his blog more than a year ago <a href="http://blog.stuartherbert.com/php/2007/10/16/microsofts-first-php-extension-sql-server-2005-support/">pointed</a> some key Microsoft Sql extension for PHP pros:</p>
<blockquote><p><strong>What’s Wrong With The Existing MSSQL Extension For PHP?</strong></p>
<p>… or, why do we need an improved SQL Server extension for PHP? :)</p>
<p>The existing MSSQL extension works well, but has a few practical limitations that have to be worked around.</p>
<ul>
<li><em>Limited to varchar(255) support</em></li>
<li><em>No support for unicode columns like nvarchar</em></li>
<li><em>No PDO drivers</em></li>
<li><em>Poor error reporting</em></li>
</ul>
</blockquote>
<p>Now some of these issues are fixed, however Microsoft native Sql driver is still not used. As I&#8217;ve mentioned <a href="http://dev.juokaz.com/winphp-2009/zend-framework-and-microsoft-iis">before</a>, there are to ways to use <a href="http://www.microsoft.com/sqlserver/2005/en/us/PHP-Driver.aspx">native driver</a> in PHP now:</p>
<ol>
<li> As <a href="http://devzone.zend.com/article/1021">PHP extension</a></li>
<li>Through <a href="http://en.wikipedia.org/wiki/Open_Database_Connectivity">ODBC</a></li>
</ol>
<p>However, none of these is available in Zend Framwork. So my task now (because of <a href="http://wiki.phpconference.nl/2009_WinPHP_Challenge">Winphp competition</a>) is to come up with something what can be used to connect to Microsft Sql database with the new driver. To start with, lets look how standard DB config looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">;application/config/application.ini, ZF 1.8</span>
resources.db.adapter <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> PDO_MYSQL</span>
resources.db.params.host <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> localhost</span>
resources.db.params.username <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> usr</span>
resources.db.params.password <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> psw</span>
resources.db.params.dbname <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> zf-tutorial</span></pre></div></div>

<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/05/lfzl3021.gif"><img src="http://dev.juokaz.com/wp-content/uploads/2009/05/lfzl3021-150x150.gif" alt="ODBC" title="ODBC" width="100" height="100" style="float: right; margin-left: 5px;" class="alignnone size-thumbnail wp-image-674" /></a>My goal is to change it as minimal as possible to allow easy db&#8217;s migration. To test <a href="http://en.wikipedia.org/wiki/Open_Database_Connectivity"><strong>ODBC</strong></a> connector I downloaded this <a href="http://framework.zend.com/issues/browse/ZF-905">class</a> and renamed it to <strong>Add_Db_Adapter_Pdo_Odbc</strong> (because it&#8217;s not a good idea to change something in actual Zend Framework library). To use it in your code you need to change config file to something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.db.adapter <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> PDO_ODBC</span>
resources.db.params.adapterNamespace <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> App_Db_Adapter</span>
resources.db.params.username <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> usr</span>
resources.db.params.password <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> psw</span>
resources.db.params.dbname <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Driver={SQL Native Client};
Server=localhost\SQLEXPRESS;Database=zf-tutorial;&quot;</span></pre></div></div>

<p>Note <em>dbname</em> string &#8211; now it has not only database name, but also driver information and host. I guess such format is chosen because dns&#8217; for ODBC are very different for different providers, so to make things easier you just supply whole string, not it&#8217;s parts (driver, server/file, etc.). Don&#8217;t forget to use correct server host &#8211; for some strange reasons 127.0.0.1 refused to work for me.</p>
<p>Problem with ODBC is that it&#8217;s too general: ODBC supports <a href="http://www.connectionstrings.com/">a lot</a> databases, hence they each can have something different (correct me if I&#8217;m wrong). That&#8217;s why I&#8217;m thinking about refactoring this class to <em>App_Db_Adapter_Pdo_Odbc_Abstract</em> abstract class and separate classes for different drivers. Although this class works fine for MSSQL, so can be used as is.</p>
<p>To test <strong>PHP extension</strong> there are no classes at all (at least I haven&#8217;t found, only in <a href="http://adodb.sourceforge.net/">Adodb</a>). So I decided to implement one myself &#8211; with a help of <a href="http://msdn.microsoft.com/en-us/library/cc296152(SQL.90).aspx">SqlSrv API Reference</a> and <a href="http://framework.zend.com/apidoc/core/Zend_Db/Adapter/Zend_Db_Adapter_Abstract.html">Zend_Db_Adapter_Abstract</a> abstract class I quite quickly came up with a working solution <strong>App_Db_Adapter_Mssql</strong>. Connection configuration looks the same:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.db.adapter <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> MSSQL</span>
resources.db.params.adapterNamespace <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> App_Db_Adapter</span>
resources.db.params.host <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> localhost\SQLEXPRESS</span>
resources.db.params.username <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> usr</span>
resources.db.params.password <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> psw</span>
resources.db.params.dbname <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> zf-tutorial</span></pre></div></div>

<p>I&#8217;ve tested both classes with the same application from <a href="http://dev.juokaz.com/winphp-2009/zend-framework-and-microsoft-iis">previous post</a> and benchmark results surprised me. They were <strong>equal</strong>! With a small variations page generation times were about 140 <em>ms</em> (all default settings, IIS7, MSSQL Express). Why? SQL server driver for PHP is open source, so if you look at it&#8217;s source you would find (C++):</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">SQLRETURN build_connection_string_and_set_conn_attr<span style="color: #008000;">&#40;</span>
       sqlsrv_conn <span style="color: #0000ff;">const</span><span style="color: #000040;">*</span> conn, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> server, zval <span style="color: #0000ff;">const</span><span style="color: #000040;">*</span> options,
       __inout std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> connection_string TSRMLS_DC <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   ...
&nbsp;
   <span style="color: #007788;">connection_string</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;Driver={SQL Native Client};Server=&quot;</span><span style="color: #008080;">;</span>
   connection_string <span style="color: #000040;">+</span><span style="color: #000080;">=</span> server<span style="color: #008080;">;</span>
   connection_string <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;;&quot;</span><span style="color: #008080;">;</span>
&nbsp;
   ...
&nbsp;
   <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> zend_hash_internal_pointer_reset<span style="color: #008000;">&#40;</span> oht <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         zend_hash_has_more_elements<span style="color: #008000;">&#40;</span> oht <span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> SUCCESS<span style="color: #008080;">;</span>
         zend_hash_move_forward<span style="color: #008000;">&#40;</span> oht <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
      ...
&nbsp;
       <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> NO_ATTRIBUTE <span style="color: #000080;">==</span> ret.<span style="color: #007788;">attr</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
           <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> ret.<span style="color: #007788;">add</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
               connection_string <span style="color: #000040;">+</span><span style="color: #000080;">=</span> key<span style="color: #008080;">;</span>
               connection_string <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;={&quot;</span><span style="color: #008080;">;</span>
               connection_string <span style="color: #000040;">+</span><span style="color: #000080;">=</span> ret.<span style="color: #007788;">str_value</span><span style="color: #008080;">;</span>
               connection_string <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;};&quot;</span><span style="color: #008080;">;</span>
           <span style="color: #008000;">&#125;</span>
       <span style="color: #008000;">&#125;</span>
       ...
   <span style="color: #008000;">&#125;</span>
&nbsp;
   ...
&nbsp;
   <span style="color: #0000ff;">return</span> SQL_SUCCESS<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>If you are familiar with C++ you can look at full source at <a href="http://sql2k5php.codeplex.com/SourceControl/changeset/view/33567#265058">codeplex.com</a>. What this function does is it creates ODBC connection string, so basically it&#8217;s the same thing as using <a href="http://php.oregonstate.edu/manual/en/ref.pdo-odbc.php">PDO_ODBC</a> (no?). Both are implemented as extensions (not as PHP code) so there are no performance differences, test proves that.</p>
<p>After today&#8217;s analysis I can&#8217;t say which driver is better to use: ODBC or native driver. However, since <a href="http://php.oregonstate.edu/manual/en/intro.pdo.php">Pdo</a> driver is already being used in Zend Framework it&#8217;s easier and faster to use ODBC. Also, ODBC doesn&#8217;t require Sql PHP extension to be installed, so as long as you have Sql server and <a href="http://msdn.microsoft.com/en-us/data/aa937733.aspx">native client</a> it will work without <em>php.ini</em> modifications.</p>
<p>In conclusion, if you want to use SQL Native driver with Zend Framework solutions are almost here. I can promise, that something stable and proven to be working by actual application will be released before middle of June. I would love to hear comments from Microsoft people or ODBC users &#8211; I&#8217;m confused a little bit, because I didn&#8217;t have a lot of experience with Microsoft&#8217;s Sql and ODBC (you can share find me in twitter also <a href="http://twitter.com/juokaz">@juokaz</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/sql-native-client-as-mssql-driver-for-zend-framework/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Dynamic Assemblies loading using Reflection</title>
		<link>http://dev.juokaz.com/winphp-2009/dynamic-assemblies-loading-using-reflection</link>
		<comments>http://dev.juokaz.com/winphp-2009/dynamic-assemblies-loading-using-reflection#comments</comments>
		<pubDate>Fri, 24 Apr 2009 14:04:28 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[competition]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[gac]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[system.reflection]]></category>
		<category><![CDATA[winphp]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=544</guid>
		<description><![CDATA[Tuesday I&#8217;ve posted post how to use .Net assemblies in PHP, however, as Teal&#8217;c correctly pointed out:
Making your library COM visible requires that you register the DLL in the GAC and registering something in the GAC should only be done when multiple applications need to access the same version of the same library. Even then, [...]]]></description>
			<content:encoded><![CDATA[<p>Tuesday I&#8217;ve posted post how to use <a href="http://dev.juokaz.com/winphp-2009/using-php-with-c-written-libraries">.Net assemblies in PHP</a>, however, as Teal&#8217;c correctly pointed out:</p>
<blockquote><p><img src="http://dev.juokaz.com/wp-content/uploads/2009/04/picture.png" alt=".NET" title=".NET" width="48" height="48" class="alignnone size-full wp-image-566" style="float: right;" />Making your library COM visible requires that you register the DLL in the GAC and registering something in the GAC should only be done when multiple applications need to access the same version of the same library. Even then, most software houses will deploy each app with it’s own copy of the DLL instead of using the GAC.</p></blockquote>
<p>Nevertheless, C# (and most of other languages) support <a href="http://en.wikipedia.org/wiki/Reflection_(computer_science)">Reflection</a>. Basically using reflection, programs can &#8220;observe and modify its own structure and behavior&#8221; (wiki). .NET has <a href="http://msdn.microsoft.com/en-us/library/system.reflection.aspx">System.Reflection</a> namespace with all needed methods. Purpose of this post is to find a way how to load DLL&#8217;s dynamically &#8211; without worrying about registering, re-registering, unregistering them in running system/production servers. </p>
<p>DLL&#8217;s are library assemblies, which &#8220;contain code in CIL, which is usually generated from .NET languages, and then compiled into machine language at runtime by the CLR just-in-time compiler.&#8221; (<a href="http://en.wikipedia.org/wiki/.NET_assembly">from</a>). For all not familiar with assemblies, assembly &#8220;is a reusable, versionable, and self-describing building block of a common language runtime application.&#8221; (<a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx">from</a>). Consider this code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> Php
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Reflection
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">Object</span> get<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> assembly, <span style="color: #FF0000;">string</span> className<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Assembly asm <span style="color: #008000;">=</span> Assembly.<span style="color: #0000FF;">LoadFrom</span><span style="color: #000000;">&#40;</span>assembly<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> asm.<span style="color: #0000FF;">CreateInstance</span><span style="color: #000000;">&#40;</span>className, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is very basic class, which can be used to do such things as:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Php.<span style="color: #0000FF;">Reflection</span> obj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Php.<span style="color: #0000FF;">Reflection</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">Object</span> my <span style="color: #008000;">=</span> obj.<span style="color: #0000FF;">get</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;my.dll&quot;</span>, <span style="color: #666666;">&quot;namespace.class&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>You may have thought that this class is absolutely useless, because you can simply use <a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx"><em>System.Reflection.Assembly</em></a> without wrapping it into separate class. However, I was writing this code with an intention to use it as <a href="http://uk2.php.net/manual/en/class.com.php">COM</a> object in PHP and because PHP can&#8217;t access static COM class methods (which I find very disappointing) I needed to create this class. Now in PHP you can write:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Com <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Php.Reflection&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$math</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;math_class.dll&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;phpclass3.first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/dotnet_global_assembly_cache.gif"><img src="http://dev.juokaz.com/wp-content/uploads/2009/04/dotnet_global_assembly_cache-150x150.gif" alt="dotnet_global_assembly_cache" title="dotnet_global_assembly_cache" width="100" height="100" style="float: right; margin-left: 5px;" class="alignnone size-thumbnail wp-image-572" /></a>This method, in my opinion, is much much more better than registering every single class with <a href="http://en.wikipedia.org/wiki/Global_Assembly_Cache">global assembly cache</a>. It&#8217;s more practical and maintainable to have all required libraries in the same project directory, and not somewhere in Windows core. However, benchmarks are still coming so stay <a href="http://dev.juokaz.com/feed">tuned</a>.</p>
<p>* <em> For everyone interested in Winphp contest, and people building PHP apps in Windows, I recommend reading <a href="http://www.eurowinphp.com/">www.eurowinphp.com</a> blog, which aggregates all participants blogs. For example, Timmy Kokke, another WinPHP participant, recently wrote another great tutorial <a href="http://geekswithblogs.net/tkokke/archive/2009/04/24/how-to-use-.net-assemblies-in-php.aspx">how to make your .NET classes accessible in PHP</a>. Also, you can follow my on Twitter at <a href="http://twitter.com/juokaz">twitter.com/juokaz</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/dynamic-assemblies-loading-using-reflection/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using PHP with C# written libraries</title>
		<link>http://dev.juokaz.com/winphp-2009/using-php-with-c-written-libraries</link>
		<comments>http://dev.juokaz.com/winphp-2009/using-php-with-c-written-libraries#comments</comments>
		<pubDate>Wed, 22 Apr 2009 14:59:48 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[com]]></category>
		<category><![CDATA[comvisible]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[iis]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows server]]></category>
		<category><![CDATA[windows xp]]></category>
		<category><![CDATA[winphp]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=509</guid>
		<description><![CDATA[One of the biggest issue for me in WinPHP challenge (more info) is using C# written libraries in PHP. Actual PHP part is very easy to write, because only thing you need to do is to create COM object. However, making your dll&#8217;s visible by PHP is a bit tricky. 
I followed this tutorial, which [...]]]></description>
			<content:encoded><![CDATA[<p><img style="float:left; margin-right: 2px; " src="http://dev.juokaz.com/wp-content/uploads/2009/04/q2208984400_9262.jpg" alt="C#" title="C#" width="45" height="45" class="alignnone size-full wp-image-525" />One of the biggest issue for me in <a href="http://dev.juokaz.com/winphp-2009/first-annual-winphp-challenge">WinPHP challenge</a> (<a href="http://wiki.phpconference.nl/2009_WinPHP_Challenge/Participants">more info</a>) is using C# written libraries in PHP. Actual PHP part is very easy to write, because only thing you need to do is to create <a href="http://uk2.php.net/manual/en/class.com.php">COM object</a>. However, making your <a href="http://en.wikipedia.org/wiki/Dynamic-link_library">dll</a>&#8217;s visible by PHP is a bit tricky. </p>
<p>I followed this <a href="http://www.devarticles.com/c/a/PHP/Using-the-.NET-Assembly-in-PHP/">tutorial</a>, which has most of required information. Firstly, you create your &#8220;Class library&#8221;, then create <a href="http://support.microsoft.com/kb/815808">assembly key file</a> and assign it to project assembly. After project is built, you register and add to global assembly cache your library. Probably it&#8217;s quite natural process for all .NET developers, but for PHP developer like me, registering something with whole system seemed quite odd (<a href="http://en.wikipedia.org/wiki/DLL_hell">dll hell?</a>).</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/visualstudio-1.jpg"><img class="alignnone size-thumbnail wp-image-510" title="visualstudio-1" style="float: right; margin-left: 5px;" src="http://dev.juokaz.com/wp-content/uploads/2009/04/visualstudio-1-150x150.jpg" alt="visualstudio-1" width="100" height="100" /></a>First problem was <strong><a href="http://msdn.microsoft.com/en-us/library/ms182157(VS.80).aspx">ComVisible</a></strong>. ComVisible is a keyword in assembly which is used to control types visibility: &#8220;If you need to access a type in this assembly from COM, set the ComVisible attribute to true on that type&#8221;. VisualStudio by default sets it to false, so all types are hidden and trying to register dll will result in this warning:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">RegAsm : warning RA0000 : No types were registered</pre></div></div>

<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/visualstudio-4.jpg"><img src="http://dev.juokaz.com/wp-content/uploads/2009/04/visualstudio-4-150x150.jpg" style="float: right; margin-left: 5px;" alt="visualstudio-4" title="visualstudio-4" width="100" height="100" class="alignnone size-thumbnail wp-image-533" /></a>Luckily, I find out about ComVisible property quite soon &#8211; it&#8217;s quite hard to know about it if you haven&#8217;t done anything serious with .NET. Nevertheless, after setting it to <em>true</em>, everything should work fine. Sometimes VisualStudio (2008 Professional Edition) refused to build project because of locked dll, so don&#8217;t forget to try to kill <a href="http://uk2.php.net/security.cgi-bin">PHP-CGI</a> process first (Ctrl+Alt+Del). I didn&#8217;t had any other problems.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/visualstudio-3.jpg"><img class="alignnone size-thumbnail wp-image-512" title="visualstudio-3" style="float: right; margin-left: 5px;"  src="http://dev.juokaz.com/wp-content/uploads/2009/04/visualstudio-3-150x150.jpg" alt="visualstudio-3" width="100" height="100" /></a>After some tests and failures I ended up with two simple C# classes and PHP caller. <em>Class1</em> has two public variables (<em>x</em>, <em>y</em>) which are set by PHP in <em>$math</em> object. Method <em>Class1.sum()</em> creates new <em>Math</em> object with constructor parameters <em>x</em> and <em>y</em>, and calls getSum() which returns sum of <em>x</em> and <em>y</em>. Useless stuff, but it worked great to test if:</p>
<ul>
<li>COM objects works</li>
<li>One class can call another (Class1 -> Math)</li>
<li>Types are correctly understood/converted</li>
</ul>
<p>Working PHP code (<em>phpclass2</em> is C# library namespace):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$math</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> COM <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;phpclass2.Class1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$math</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$math</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;sum = &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$math</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sum</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$math</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Which successfully outputted:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">sum <span style="color: #339933;">=</span> <span style="color: #cc66cc;">200</span></pre></div></div>

<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/iis-1.jpg"><img class="alignnone size-thumbnail wp-image-511" title="iis-1" style="float: right; margin-left: 5px;" src="http://dev.juokaz.com/wp-content/uploads/2009/04/iis-1-150x150.jpg" alt="iis-1" width="100" height="95" /></a>I tried this code with the same <a href="http://www.microsoft.com/web/platform/default.aspx">Web Platform</a> I <a href="http://dev.juokaz.com/winphp-2009/setting-up-windows-for-php-server">used</a> in Windows Server 2008, but now with older IIS (v5, Windows XP) and it worked perfectly with all default settings. Also, IIS control panel in Server 2008 is 10 times better than in Windws Xp &#8211; in Xp-one I couldn&#8217;t even find how to add new virtual host (my bad, but in IIS 7 it&#8217;s way easier to do). And because I was installing it second time, it took me only few minutes.</p>
<p>Getting this to work was probably one of the biggest milestones &#8211; actual frontend code and <a href="http://msdn.microsoft.com/en-us/library/cc645050(VS.95).aspx">Deepzoom</a> C# library is not that tricky. After some days of working with PHP+Windows things are starting to get a shape &#8211; except of different OS GUI, web server works good in both Linux and Windows &#8211; clearly Microsoft is holding this competition to show it. And they are in the right way.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/using-php-with-c-written-libraries/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Setting up Windows for PHP server</title>
		<link>http://dev.juokaz.com/winphp-2009/setting-up-windows-for-php-server</link>
		<comments>http://dev.juokaz.com/winphp-2009/setting-up-windows-for-php-server#comments</comments>
		<pubDate>Tue, 21 Apr 2009 11:37:35 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[WinPhp 2009]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[iis]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[skype]]></category>
		<category><![CDATA[virtualbox]]></category>
		<category><![CDATA[web platform]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows server]]></category>
		<category><![CDATA[winphp]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=469</guid>
		<description><![CDATA[To start with, all work took about 1:30, including Skype distractions, reading how-to&#8217;s, etc. Only one problem I had is that I was given remote desktop connection access to Windows Server 2008 machine, but I&#8217;m working with Linux. How to connect to remote desktop from Linux?
Luckily, I had Windows XP in VirtualBox so I tried [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-493" style="float: left; margin-right: 5px;" title="iis_logo" src="http://dev.juokaz.com/wp-content/uploads/2009/04/iis_logo.png" alt="iis_logo" width="160" height="76" />To start with, all work took about 1:30, including <a href="http://www.skype.com">Skype</a> distractions, reading how-to&#8217;s, etc. Only one problem I had is that I was given <a href="http://www.microsoft.com/windowsXp/using/mobility/getstarted/Remoteintro.mspx">remote desktop</a> connection access to Windows Server 2008 machine, but I&#8217;m working with Linux. How to connect to remote desktop from Linux?</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-15.png"><img class="alignnone size-thumbnail wp-image-488" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-15" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-15-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-15" width="100" height="100" /></a>Luckily, I had Windows XP in <strong><a href="http://www.virtualbox.org/">VirtualBox</a></strong> so I tried it. And it worked really well. I&#8217;m using <a href="http://www.ed.ac.uk">university</a> internet network so connections speeds are very fast and Ubuntu -&gt; Windows Xp -&gt; Windows Server 2008 worked just fine. Later I will probably use real Windows system, but for first steps virtual machine worked much more faster than I expected.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-3.png"><img class="size-thumbnail wp-image-475" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-3" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-3-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-3" width="100" height="99" /></a> Almost 4 years ago I had experience installing <strong><a href="http://en.wikipedia.org/wiki/Internet_Information_Services">IIS</a></strong> server, but I haven&#8217;t done much since then. Nevertheless, installing IIS+PHP was really easy &#8211; I followed <a href="http://blogs.msdn.com/bramveen/archive/2009/04/20/creating-your-php-dev-environment-for-the-winphp-challenge.aspx">this</a> tutorial, which shows you how to install <a href="http://www.microsoft.com/web/platform/default.aspx">Microsoft Web Platform</a>, and IIS was ready. Platform installer allows you to install not only SQL, PHP, ASP.NET, etc. but also various web applications &#8211; <a href="http://www.wordpress.org">Wordpress</a>, <a href="http://drupal.org/">Drupal</a> and many more. Experience with IIS was very similar to <a href="http://dev.juokaz.com/php/zend-server-complete-php-environment-in-minutes">Zend Server</a>, but IIS control panel is way better.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-8.png"><img class="size-thumbnail wp-image-478" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-8" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-8-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-8" width="100" height="100" /></a>I chose to install Wordpress, just to test if IIS is working properly, but Platform installer doesn&#8217;t install <strong>MySQL</strong>. PHP is installed with MySQL extension, but no server is being installed &#8211; quite illogical. However, installing MySQL server is as easy as installing any other windows application (subjective) &#8211; download installer, Next, Next, Next and server is running. Also, <em>mysql</em> tool works the same in Windows <a href="http://en.wikipedia.org/wiki/Windows_command_line">cmd</a> and Linux console.</p>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-10.png"><img class="alignnone size-thumbnail wp-image-491" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-10" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-10-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-10" width="100" height="100" /></a>Tutorials and other information:</p>
<ul>
<li> <a href="http://blogs.msdn.com/bramveen/archive/2009/04/20/creating-your-php-dev-environment-for-the-winphp-challenge.aspx">Creating your PHP dev environment for the WinPHP Challenge </a></li>
<li> <a href="http://www.microsoft.com/web/downloads/platform.aspx">Microsoft Web Platform</a></li>
<li><a href="http://learn.iis.net/page.aspx/610/setting-up-mysql-for-php-applications/">Setting up MySQL for PHP applications</a></li>
<li><a href="http://php.iis.net/">PHP on IIS7</a></li>
</ul>
<p><a href="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-14.png"><img class="alignnone size-thumbnail wp-image-485" style="float: right; margin-left: 5px;" title="screenshot-windows-xp-running-sun-xvm-virtualbox-14" src="http://dev.juokaz.com/wp-content/uploads/2009/04/screenshot-windows-xp-running-sun-xvm-virtualbox-14-150x150.png" alt="screenshot-windows-xp-running-sun-xvm-virtualbox-14" width="101" height="101" /></a><br />
What I liked very much is GUI for web server control.  Currently I have 13 web apps running in my laptop and every app has different port (localhost:80, localhost:81, localhost:82, etc.), but I haven&#8217;t found any good tool for adding more sites. I would like to have simple tool, which would accept port number and directory, and create virtual host. IIS control application allows it very easily.</p>
<p>In conclusion, working with PHP in Windows server seems to be pretty acceptable. I haven&#8217;t tested setting up anything except Wordpress, but Wordpress seems to work just fine. Especially I liked using GUI, because most of tasks for development machine can be done much more easier and faster, still I&#8217;m not going to switch from Linux yet.</p>
<p><strong>Advantages</strong>:</p>
<ul>
<li>GUI</li>
<li>Easy to setup</li>
<li>PHP works</li>
</ul>
<p><strong>Disadvantages</strong>:</p>
<ul>
<li>Some functionality is hard to find</li>
<li>Differences from Apache (<a href="http://www.petefreitag.com/item/286.cfm">mod_rewrite</a>, etc.)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/winphp-2009/setting-up-windows-for-php-server/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
