<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text">php developer blog</title>
  <updated>Thu, 18 Mar 2010 21:32:08 +0000</updated>
  <id>http://www.phpdevblog.net/feeds/atom/</id>
  <link rel="alternate" type="text/html" hreflang="de" href="http://www.phpdevblog.net/"/>
  <link rel="self" type="application/atom+xml" href="http://www.phpdevblog.net/feeds/atom/"/>
  <generator version="'Ruby' Alpha 3">MCW.blog</generator>

	<entry>
    <id>http://www.phpdevblog.net/2010/03/extjs-global-shortcuts-eg-close-current-tab.html</id>
    <title>Ext.JS: Global shortcuts (e.g. close current tab)</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2010/03/extjs-global-shortcuts-eg-close-current-tab.html"/>
    <updated>2010-03-17T19:41:21Z</updated>
    <published>2010-03-17T19:41:21Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Ext.JS has an easy way of configuring keyboard shortcuts using Ext.KeyMap. If you wanna use global shortcuts you simply have to bind it to document. I wanna show you in an example how you can bind Alt+X to closing the current tab of a TabPanel layout accessible by the JavaScript variable tabPanelLayout.</p><pre name="code" class="javascript">new Ext.KeyMap(<br />  document,<br />  [<br />    {<br />      // Alt + X: Close current tab<br />      key: 'x',<br />      alt: true,<br />      // Prevent any browser actions triggered by the shortcut that may occur<br />      stopEvent: true,<br />      fn: function() {<br />        var activePanel = tabPanelLayout.getActiveTab();<br />        Layout.closeTab(activePanel);<br />      }<br />    }<br />  ]<br />);<br /></pre><p>That's about it! If you only have this one shortcut, you can leave out the JS array.</p>
      </div>
    </content>
    	<category term="development"/>
    	<category term="Ext.JS"/>
    	<category term="javascript"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/12/oop-in-javascript-part-4-scoping.html</id>
    <title>OOP in JavaScript - Part 4: Scoping</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/12/oop-in-javascript-part-4-scoping.html"/>
    <updated>2009-12-21T12:00:16Z</updated>
    <published>2009-12-21T12:00:16Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>I agree, it took me a while to write part 4 of my OOP in JavaScript series, but here it is now. As for the whole series, basic JavaScript knowledge is needed.</p><p>Scoping together with OOP is always an issue, when using asynchronous calls combined with callbacks. Let's create a very easy example where one method of a class registers an onclick EventListener and the second method of the class is the callback for the EventListener. The second method simply calls an alert with a message text defined in the class. Note that the Event Listener registration is not cross-browser safe:</p><pre name="code" class="javascript">function Handler()<br />{<br />}<br /><br />Handler.prototype.messageText = 'You have clicked somewhere in the document';<br /><br />Handler.prototype.registerAll = function()<br />{<br />        window.addEventListener('click', this.handleOnclick, true);<br />}<br /><br />Handler.prototype.handleOnclick = function(event)<br />{<br />        alert(this.messageText);<br />}<br /><br />var handler = new Handler();<br />handler.registerAll();<br /></pre><p>When clicking anywhere in the document you'd be expecting to see a message box "You have clicked somewhere in the document", but you only get a mesage box saying "undefined". Why is that?</p><p>The second argument of addEventListener() is the callback, it expects a function. By putting this.handleOnclick as callback, you are passing a <strong>copy</strong> of that function. It would be same if you had written it like this:</p><pre name="code" class="javascript">window.addEventListener('click', function() { alert(this.messageText); }, true);<br /></pre><p>As you can clearly see now the function stands for itself, this is not in the context(=scope) of the class Handler. In order to achieve exactly that, JavaScript gives us 2 possibilities: apply() and call(). Basically they are both the same, the only difference is in the way you pass further arguments. For a closer distinction, <a href="http://www.webreference.com/js/column26/apply.html">take a look at this page</a>.</p><p>By passing an instance as first argument we are telling the function that is called in which scope to be executed, meaning: What instance is <strong>this</strong>? Let's alter the registerAll method of the example:</p><pre name="code" class="javascript">Handler.prototype.registerAll = function()<br />{<br />        window.addEventListener('click', this.handleOnclick.apply(this), true);<br />}<br /></pre><p>When running your page now, you will notice that the alert puts out the right message, but is called the moment your page is rendered and the onclick event doesn't work anymore. This is because apply() and call() directly call the respecting function.</p><p>What we need to do, is to dynamically create a function that is called with the right scope when needed.</p><pre name="code" class="javascript">function Handler()<br />{<br />}<br /><br />Handler.prototype.messageText = 'You have clicked somewhere in the document';<br /><br />Handler.prototype.registerAll = function()<br />{<br />        window.addEventListener('click', this.createOnclickHandler(), true);<br />}<br /><br />Handler.prototype.createOnclickHandler = function()<br />{       <br />        var myScope = this;<br />        return function(event) {<br />                var handle = function(event) {<br />                        alert(this.messageText);<br />                }<br />                handle.call(myScope, event);<br />        }<br />}<br /><br />var handler = new Handler();<br />handler.registerAll();<br /></pre><p>Finally we get to see a "You have clicked somewhere in the document" message box only when clicking somwhere in the document. Note that the second parameter of call() is the Event Object, which is passed as first argument to the called function.</p><p>If using <strong>this</strong> is not total must for you, you can also throw away one of the functions and simply use myScope instead of this:</p><pre name="code" class="javascript">Handler.prototype.createOnclickHandler = function()<br />{       <br />        var myScope = this;<br />        return function(event) {<br />                alert(myScope.messageText);<br />        }<br />}<br /></pre>
      </div>
    </content>
    	<category term="browser"/>
    	<category term="javascript"/>
    	<category term="oop"/>
    	<category term="web"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode-memory-usage.html</id>
    <title>serialize() vs. var_export() vs. json_encode() Memory Usage</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode-memory-usage.html"/>
    <updated>2009-11-18T10:24:24Z</updated>
    <published>2009-11-18T10:24:24Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Based on <a href="2009/11/serialize-vs-var-export-vs-json-encode-part-2.html">serialize() vs. var_export() vs. json_encode() Part 2</a>, let's take a look at the (real) memory usage:</p><p style="text-align: center;"><img src="images/serialize3-memory-line.jpg" alt="" width="485" height="509" /></p><p style="text-align: left;">Apart from var_export allo methods seem to use about the same. Let's put the usage in relation to each other to get a better view:</p><p style="text-align: center;"><img src="images/serialize3-memory-bar.jpg" alt="" width="485" height="509" /></p><p style="text-align: left;">With smaller arrays, the memory usage is exactly the same, from 10.000 elements on the values begin to diverge.</p>
      </div>
    </content>
    	<category term="php"/>
    	<category term="profiling"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode-part-2.html</id>
    <title>serialize() vs. var_export() vs. json_encode() Part 2</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode-part-2.html"/>
    <updated>2009-11-17T17:36:20Z</updated>
    <published>2009-11-17T17:36:20Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Based on the comments from my first benchmark in the <a href="2009/11/serialize-vs-var-export-vs-json-encode.html">serialize() vs. var_export() vs. json_encode</a> article 2 days ago, I decided to benchmark once again, this time using different array sizes and I also added the JSON method with recursive UTF8 encoding beforehand. This time, all results are the combined results (exporting and importing), the testing script was exactly the same (except JSON+UTF8 which also had a utf8_encode_recursive function). Let's see what happens when using array sizes from 10 to 1000 elements:</p><p style="text-align: center;"><img src="images/serialize2-low.jpg" alt="" width="485" height="509" /></p><p style="text-align: left;">So far, json_encode itself is fastest, closely followed by serialization. JSON with UTF8 Encoding is slowest, but consider that all the values are all below 0,02 seconds.</p><p style="text-align: left;">So far so good, let's look at the results with arrays from 10.000 to 1.000.000 elements:</p><p style="text-align: center;"><img src="images/serialize2-high.jpg" alt="" width="485" height="509" /></p><p style="text-align: left;">Apart from the exception Serialization the values mostly develop like with smaller arrays. What's quite odd is that Serialization's runtime seems to be exploding when stepping up from 100.000 to 1.000.000. Why that is so, I cannot tell. Although JSON with UTF8 Encoding is slower than var_export and JSON itself, it's still way faster than serialization.</p><p style="text-align: left;"><strong>Conclusions</strong></p><ol><li>None of the methods scales linear.</li><li>For smaller arrays, JSON is the way to go, as long as your data is already UTF8 encoded. If not, you might want to take serialization.</li><li>With larger arrays JSON is still fastest as long as your data is already UTF8 encoded, otherwise var_export is the best choice.</li></ol><p>Closing, let's take a look at the graphs from 10 - 1.000.000 item large arrays:</p><p style="text-align: center;"><img src="images/serialize2-all.jpg" alt="" width="485" height="509" /></p>
      </div>
    </content>
    	<category term="php"/>
    	<category term="profiling"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html</id>
    <title>serialize() vs. var_export() vs. json_encode()</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/11/serialize-vs-var-export-vs-json-encode.html"/>
    <updated>2009-11-16T02:30:02Z</updated>
    <published>2009-11-16T02:30:02Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>There are times when you need to store an array, for example when your array is an index you wanna use again the next time you run your application. In order to store an array you have to transform it into some kind of string represantation first, most people would probably use <a href="http://de2.php.net/manual/en/function.serialize.php" target="_blank">serialize()</a>. But there are also 2 other ways to achieve that: <a href="http://de2.php.net/manual/en/function.var-export.php" target="_blank">var_export()</a> and <a href="http://de2.php.net/manual/en/function.json-encode.php" target="_blank">json_encode()</a>.</p><p>After having them stored the functions to interpret the strings as arrays would be unserialize() if you use serialize(), eval() if you use var_export() and json_decode() if you use json_encode().</p><p>So, what about the performance?</p><p>In order to test that I wrote a little profiling script that first created some random array with 1.000.000 elements, then exported the array and then imported it again. For the json_encode() test the script looked like that:</p><pre name="code" class="php">$array = array_fill(0, 1000000, rand(1, 9999));<br /><br />$start = microtime(true);<br />$export = json_encode($array);<br />$end = microtime(true);<br />$duration = $end - $start;<br />print('JSON Encode: ' . $duration . PHP_EOL);<br /><br />$start = microtime(true);<br />$import = json_decode($export);<br />$end = microtime(true);<br />$duration = $end - $start;<br />print('JSON Decode: ' . $duration . PHP_EOL);<br /><br /></pre><p>Apart from the exporting and importing functions used, the script for serialize() and var_export() looked pretty much the sime, var_export() being the only exception, since I had to add an ending ; to $export in order for it to work with eval().</p><p>While it is understandable that importing takes longer for every method than importing, the differences in time are quite astounding:</p><p style="text-align: center;"><img src="images/serialize-seperate.jpg" alt="" width="485" height="509" /></p><p>It's not only interesting to see that unserialize() is damn slow but also that JSON is fastest, which also gets quite clear when looking at combined results:</p><p style="text-align: center;"><img src="images/serialize-combined.jpg" alt="" width="485" height="509" /></p><p style="text-align: center;"> </p><p style="text-align: left;">Since it's still in the Ubuntu repositories, I did the performance tests with PHP 5.2.6</p>
      </div>
    </content>
    	<category term="php"/>
    	<category term="profiling"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/11/speaking-at-php-conference-2009-about-scrum.html</id>
    <title>Speaking at PHP Conference 2009 about Scrum</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/11/speaking-at-php-conference-2009-about-scrum.html"/>
    <updated>2009-11-15T00:45:33Z</updated>
    <published>2009-11-15T00:45:33Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Today (November, 15th) I'm giving a workshop about Scrum basics at the PHP Conference 2009 in Karlsruhe, Germany together with my colleague Sebastian Schürmann. If you're new to scrum and/or have never heard about it, it should be a quite interesting workshop for you.</p>
      </div>
    </content>
    	<category term="general"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/11/article-mobile-webapplications-in-german-php-magazin.html</id>
    <title>Article "Mobile Webapplications" in german PHP Magazin</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/11/article-mobile-webapplications-in-german-php-magazin.html"/>
    <updated>2009-11-11T10:14:13Z</updated>
    <published>2009-11-11T10:14:13Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>As of today <a href="http://it-republik.de/php/php-magazin-ausgaben/Get-mobile!-000331.html">issue 1.2010 of the german "PHP Magazin"</a> is available, containing my article "Mobile Webapplications" with tips &amp; tricks for developing your own Mobile Webapplication using PHP.</p><p>As for the "OOP in JavaScript" series: I didn't forget about it, I just didn't have enough time to write the fourth part of it which will be covering scoping.</p>
      </div>
    </content>
    	<category term="general"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/09/oop-in-javascript-part-3-extending-classes.html</id>
    <title>OOP in JavaScript - Part 3: Extending classes</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/09/oop-in-javascript-part-3-extending-classes.html"/>
    <updated>2009-09-22T15:07:09Z</updated>
    <published>2009-09-22T15:07:09Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>After I've already covered <a href="2009/09/oop-in-javascript-part-1-writing-classes.html">norma</a>l and <a href="2009/09/oop-in-javascript-part-2-static-classes-and-singleton.html">static classes</a>, Part 3 is about extending classes. As before, basic knowledge in OOP and JavaScript is needed.</p><p>First, let's create a class Human with setters and getters for the age:</p><pre name="code" class="javascript">function Human()<br />{<br />	<br />}<br /><br />Human.prototype._age = 0;<br /><br />Human.prototype.setAge = function(age)<br />{<br />	if (typeof age != 'number') {<br />		throw('Invalid Age provided');<br />	}<br />	this._age = age;<br />};<br /><br />Human.prototype.getAge = function()<br />{<br />	return this._age;<br />};<br /></pre><p>Since there are no keywords like class, private, public or protected in Javascript it's no surprise there's no keyword extends aswell. In order to extend a class you have to use prototyping:</p><pre name="code" class="javascript">function Woman(age)<br />{<br />    this.setAge(age);<br />}<br /><br />Woman.prototype = new Human();<br /></pre><p>Now we've created a new class Woman that inherits all methods and properties from Human by assigning Woman's prototype to <strong>new Human()</strong>. To show it's easily possible, I've also changed the constructor as it now takes age as parameter. Creating a new object Woman and reading the age looks like this:</p><pre name="code" class="javascript">var woman = new Woman(22);<br />alert(woman.getAge());<br /></pre><p>You can overload methods simply by redifining them. In this case we'll overload the getAge method where we'll add an additional check and then call the parent getAge method. Since you actually don't call the parent method but call the parent method in static way, it is important to add <strong>call(this)</strong> in order to call the method in the actual context.</p><pre name="code" class="javascript">Woman.prototype.getAge = function()<br />{<br />        if (this._age &gt; 100) {<br />            throw('This person is probably already dead!');<br />        }<br />	return Human.prototype.getAge.call(this);<br />};<br /></pre><p>Multiple inheritance is possible, but not very easy. For a method using Swiss Inheritance, <a href="http://www.crockford.com/javascript/inheritance.html" target="_blank">check this page</a>. Extending static classes might be possible, but I don't know a way. Problem is you need to clone the parent class (which is not that easy in JavaScript), if you write it like this the child becomes the parent and the other way round, meaning if you add a method in the Child it is also available in the parent:</p><pre name="code" class="javascript">function Cookie()<br />{<br />	<br />}<br /><br />Cookie.get = function()<br />{<br />	return 'something';<br />};<br /><br />var CookieChild = Cookie;<br />alert(CookieChild.get());<br /></pre><p>Part 4 (probably the last one of the series for now) will cover scoping.</p>
      </div>
    </content>
    	<category term="browser"/>
    	<category term="javascript"/>
    	<category term="oop"/>
    	<category term="web"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/09/oop-in-javascript-part-2-static-classes-and-singleton.html</id>
    <title>OOP in JavaScript - Part 2: Static classes and Singleton</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/09/oop-in-javascript-part-2-static-classes-and-singleton.html"/>
    <updated>2009-09-15T11:57:34Z</updated>
    <published>2009-09-15T11:57:34Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>After I've covered normal classes <a title="OOP in JavaScript - Part 1: Writing classes" href="2009/09/oop-in-javascript-part-1-writing-classes.html">in Part 1 of the series</a>, the second part will focus on static classes and singletons. As before basic OOP and JavaScript knowledge is needed.</p><p>Generally writing and using static classes is monstly the same as normal classes. The difference is that you don't need to instantiate the object with <strong>new </strong>and you can't use prototyping nor <strong>this</strong>, since everything is in static context. One useful implementation for static classes is the Registry Design Pattern. First off we create the class with a property to store the registry values in afterwards:</p><pre name="code" class="javascript">function Registry()<br />{<br />}<br /><br />Registry._data = {};<br /></pre><p>Right now, I can't really do a lot with this class, that's why I'll create a static method that allows me to set key-value-combinations:</p><pre name="code" class="javascript">Registry.set = function(key, value) {<br />    Registry._data[key] = value;<br />}<br /></pre><p>Now I'm able to register stuff in the registry. Since I don't need an instance of the class I can call my method directly:</p><pre name="code" class="javascript">Registry.set('userIsAuthorized', false);<br /></pre><p>I'll leave out the getter you'd still need for a useful registry.</p><p>If you want to prevent anyone from writing <strong>new Registry()</strong> you only have to throw an exception in the constructor:</p><pre name="code" class="javascript">function Registry()<br />{<br />    throw('You cannot instantiate Registry');<br />}<br /></pre><p>Maybe for some reason you wanna use Registry as a <strong>singleton</strong>. In that case you have to combine normal class methods and properties with static ones. You write the Registry as a normal class and implement one static property instance and one static method <strong>getInstance</strong>. The Registry class would then look like this:</p><pre name="code" class="javascript">function Registry()<br />{<br />}<br /><br />Registry.instance = null;<br /><br />Registry.getInstance = function() {<br />    if (Registry.instance === null) {<br />        Registry.instance = new Registry();<br />    }<br />    return Registry.instance;<br />}<br /><br />Registry.prototype._data = {};<br /><br />Registry.prototype.set = function(key, value) {<br />    this._data[key] = value;<br />}<br /></pre><p>Since JavaScript doesn't have access modifiers like private, public or protected I had to remove the exception from the constructor. You could do it with a randomly generated token passed to the constructor, but this method is not bulletproof like any other I can think of.</p><p>In part 3 (that'll come later this week) I will write about extending classes.</p>
      </div>
    </content>
    	<category term="browser"/>
    	<category term="javascript"/>
    	<category term="oop"/>
    	<category term="web"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/09/oop-in-javascript-part-1-writing-classes.html</id>
    <title>OOP in JavaScript - Part 1: Writing classes</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/09/oop-in-javascript-part-1-writing-classes.html"/>
    <updated>2009-09-12T14:19:10Z</updated>
    <published>2009-09-12T14:19:10Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>OOP in JavaScript works a bit different than known from other languages. That's why I decided to do a series of "OOP in JavaScript" blogposts, where I want to show how OOP in JavaScript works. The series will discuss topics like creating classes, prototyping, extending classes, static classes, scoping, and so on. To understand these postings a basic knowledge of JavaScript and OOP is needed. This first part is about creating classes/prototyping.</p><p>First thing you need to know: There is now keyword <strong>class</strong> in JavaScript. In order to create a class in JavaScript, all you have to do is to create a function and then create a new class instance by using the known keyword <strong>new</strong>:</p><pre name="code" class="javascript">function Product() {}<br /><br />var product = new Product();<br /></pre><p>The function we've created is the <strong>constructor </strong>of our new class. To create methods and properties there are two ways: You can either declare them in the constructor or you can do prototyping.</p><p>Declaring it in the constructor:</p><pre name="code" class="javascript">function Product() {<br />    this.id = 71288;<br /><br />    this.getId = function() {<br />        return this.id;<br />    }<br />}<br /></pre><p>Prototyping:</p><pre name="code" class="javascript">function Product() {}<br /><br />Product.prototype.id = 71288;<br /><br />Product.prototype.getId = function() {<br />    return this.id;<br />}<br /></pre><p>Personally I prefer prototyping as it is more clear. If you want to call the function getId() it always works the same way with both methods:</p><pre name="code" class="javascript">var product = new Product();<br />alert(product.getId());<br /></pre><p>This class is still quite static, since the id is hardcoded in it. To make it dynamic I just need to modify the constructor to make it accept a config object. Notice that I replaced the id property with a config property that stores the whole config given to the object.</p><pre name="code" class="javascript">function Product(config) {<br />    this.parseConfig(config);<br />}<br /><br />Product.prototype.config = {};<br /><br />Product.prototype.parseConfig = function(config) {<br />    if (typeof config != 'object') {<br />        throw('Parameter "config" must be of type object');<br />    }<br />    // ID should always be set and a number<br />    if (typeof config.id != 'number') {<br />        throw('Config Parameter config.id must be a number');<br />    }<br />    this.config = config;<br />}<br /><br />Product.prototype.getId = function() {<br />    return this.config.id;<br />}<br /></pre><p>As you can see I also already implemented checks that the config must always be an object and that an id must always be set and a number. Creating a new instance of the class now looks like this:</p><pre name="code" class="javascript">var product = new Product({<br />    id: 71288<br />});<br /></pre><p>This way we can now dynamically create Product Objects based on Product IDs we read from e.g. a database.</p><p>In the second part, that will come within the next few days I will write about static classes.</p>
      </div>
    </content>
    	<category term="browser"/>
    	<category term="javascript"/>
    	<category term="oop"/>
    	<category term="web"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/08/haystack-needle-sheet.html</id>
    <title>Haystack-Needle-Sheet</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/08/haystack-needle-sheet.html"/>
    <updated>2009-08-04T09:45:27Z</updated>
    <published>2009-08-04T09:45:27Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Surely every PHP developer has come across the PHP Haystack-Needle-Phenomenon: Mostly the parameter order is $haystack, $needle but in some cases it's $needle, $haystack for no apparent reason. Trying to find regularities I found out that it's only the array functions that use needle, haystack instead of haystack, needle - assuming I didn't forget any functions. For that reason I created a <strong>Haystack-Needle-Sheet</strong>, which shows you which function uses haystack, needle and which one needle, haystack.</p><p>If you should come across functions that are missing on this list, please let me know.</p><p><a href="uploads/files/haystack-needle.pdf">You can download the Haystack-Needle-Sheet here</a></p>
      </div>
    </content>
    	<category term="php"/>
    	<category term="utilities"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/07/cookies-in-javascript.html</id>
    <title>Cookies in JavaScript</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/07/cookies-in-javascript.html"/>
    <updated>2009-07-29T14:04:46Z</updated>
    <published>2009-07-29T14:04:46Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Using in Cookies is rather complicated. All the cookies are in the string <strong>document.cookie</strong>. Here's a little static class I wrote a while ago to handle saving, reading and removing Cookies in JavaScript</p><pre name="code" class="javascript">/**<br /> * Static Class for Cookie functions<br /> */<br />function Cookie()<br />{<br />}<br /><br />/**<br /> * Save new Cookie<br /> *<br /> * @param	string	name<br /> * @param	string	value<br /> * @param	number	days<br /> */<br />Cookie.save = function(name, value, days)<br />{<br />	if (typeof days != 'undefined') {<br />		var date = new Date();<br />		date.setTime(date.getTime() + (days*24*60*60*1000));<br />		var expires = "; expires=" + date.toGMTString();<br />	} else {<br />		var expires = "";<br />	}<br />	document.cookie = name + "=" + value + expires + "; path=/";<br />}<br /><br />/**<br /> * Read Cookie value<br /> *<br /> * @param	string	name<br /> */<br />Cookie.get = function(name) {<br />	var nameEQ = name + "=";<br />	var ca = document.cookie.split(';');<br />	for(var i=0;i &lt; ca.length;i++) {<br />		var c = ca[i];<br />		while (c.charAt(0)==' ') {<br />			c = c.substring(1,c.length);<br />		}<br />		if (c.indexOf(nameEQ) == 0) {<br />			return c.substring(nameEQ.length,c.length);<br />		}<br />	}<br />	return null;<br />}<br /><br />/**<br /> * Delete Cookie<br /> *<br /> * @param	string	name<br /> */<br />Cookie.remove = function(name) {<br />	Cookie.save(name,"",-1);<br />}<br /></pre>
      </div>
    </content>
    	<category term="development"/>
    	<category term="javascript"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/07/dns-cache-15-released.html</id>
    <title>DNS Cache 1.5 released</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/07/dns-cache-15-released.html"/>
    <updated>2009-07-13T12:37:40Z</updated>
    <published>2009-07-13T12:37:40Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Today the latest version 1.5 of my Firefox plugin "DNS Cache" finally went public. Apart from Firefox 3.5 compatibility the following things have changed:</p><ul><li>Explicitly flush DNS cache when deactivating</li><li>Toolbar Icon now has a context menu (right click): "Enable / Disable DNS Cache" and new:</li><li>"Flush DNS Cache": Flushes the DNS Cache no matter if the cache is disabled or not</li><li>Toolbar Icon now has to be double clicked in order to change the dns cache state</li></ul><p>You can get the Firefox addon at the <a href="https://addons.mozilla.org/de/firefox/addon/5914">Firefox Add-ons page</a>. If you've already installed it from there you should get the update automatically.</p><p>If you should experience any problems, please let me know!</p>
      </div>
    </content>
    	<category term="addon"/>
    	<category term="browser"/>
    	<category term="caching"/>
    	<category term="dns"/>
    	<category term="extension"/>
    	<category term="firefox"/>
    	<category term="web"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/06/mysql-pagination-sql-calc-found-rows-vs-count-query.html</id>
    <title>MySQL: Pagination - SQL_CALC_FOUND_ROWS vs COUNT()-Query</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/06/mysql-pagination-sql-calc-found-rows-vs-count-query.html"/>
    <updated>2009-06-17T15:51:42Z</updated>
    <published>2009-06-17T15:51:42Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>When using pagination for let's say a list of offers that where never clicked, you have to know the exact amount of offers (which were never clicked) in order to know how many pages you have. Now there are 2 possible ways of calculating the exact amount of offers: You can use either SQL_CALC_FOUND_ROWS or you can setup a second query with a COUNT() in it. I did the tests with SQL_NO_CACHE in order to get the best results possible. The clicks table has about 18.000.000 rows, the offer table about 800.000. Let's start with some time results.</p><p>Using SQL_CALC_FOUND_ROWS:</p><pre name="code" class="sql">mysql&gt; SELECT SQL_NO_CACHE SQL_CALC_FOUND_ROWS o.offer_id<br />    -&gt; FROM offer AS o<br />    -&gt; LEFT JOIN clicks AS c<br />    -&gt; ON (o.offer_id = c.offer_id)<br />    -&gt; WHERE c.offer_id IS NULL<br />    -&gt; LIMIT 50,50;<br />50 rows in set (30.84 sec)<br /><br />mysql&gt; SELECT FOUND_ROWS();<br />1 row in set (0.00 sec)<br /></pre><p>Using a second query with COUNT():</p><pre name="code" class="sql">mysql&gt; SELECT SQL_NO_CACHE o.offer_id<br />    -&gt; FROM offer AS o<br />    -&gt; LEFT JOIN clicks AS c<br />    -&gt; ON (o.offer_id = c.offer_id)<br />    -&gt; WHERE c.offer_id IS NULL<br />    -&gt; LIMIT 50,50;<br />50 rows in set (0.03 sec)<br /><br />mysql&gt; SELECT SQL_NO_CACHE COUNT(o.offer_id)<br />    -&gt; FROM offer AS o<br />    -&gt; LEFT JOIN clicks AS c<br />    -&gt; ON (o.offer_id = c.offer_id)<br />    -&gt; WHERE c.offer_id IS NULL;<br />1 row in set (30.97 sec)<br /><br /></pre><p>At a first glance they look equally fast, both taking about 30 seconds. But: They are only equally fast, when query caching is turned off. Let's assume we're on a high-traffic website where performance matters, so we turn the query cache on. MySQL Query caching is like a key-value cache with the key being the EXACT query and the resultset being the value. Once we turn on the cache, the pagination is way faster with the second query using COUNT().</p><p>Why?</p><p>When using SQL_CALC_FOUND_ROWS the application has to calculate the found rows every single time we turn the page, because the query changes, while the COUNT()-Query always remains the same, meaning that its result comes from the query cache from the second time on. Let's emulate:</p><p>Using SQL_CALC_FOUND_ROWS:</p><pre name="code" class="sql">mysql&gt; SELECT SQL_CALC_FOUND_ROWS o.offer_id<br />    -&gt; FROM offer AS o<br />    -&gt; LEFT JOIN clicks AS c<br />    -&gt; ON (o.offer_id = c.offer_id)<br />    -&gt; WHERE c.offer_id IS NULL<br />    -&gt; LIMIT 50,50;<br />50 rows in set (31.13 sec)<br /><br />mysql&gt; SELECT FOUND_ROWS();<br />1 row in set (0.00 sec)<br /><br />mysql&gt; SELECT SQL_CALC_FOUND_ROWS o.offer_id<br />    -&gt; FROM offer AS o<br />    -&gt; LEFT JOIN clicks AS c ON (o.offer_id = c.offer_id)<br />    -&gt; WHERE c.offer_id IS NULL<br />    -&gt; LIMIT 100,50;<br />50 rows in set (30.71 sec)<br /><br />mysql&gt; SELECT FOUND_ROWS();<br />1 row in set (0.00 sec)<br /></pre><p>Using a second query with COUNT():</p><pre name="code" class="sql">mysql&gt; SELECT o.offer_id<br />    -&gt; FROM offer AS o<br />    -&gt; LEFT JOIN clicks AS c<br />    -&gt; ON (o.offer_id = c.offer_id)<br />    -&gt; WHERE c.offer_id IS NULL<br />    -&gt; LIMIT 50,50;<br />50 rows in set (0.03 sec)<br /><br />mysql&gt; SELECT COUNT(o.offer_id)<br />    -&gt; FROM offer AS o<br />    -&gt; LEFT JOIN clicks AS c<br />    -&gt; ON (o.offer_id = c.offer_id)<br />    -&gt; WHERE c.offer_id IS NULL;<br />1 row in set (31.11 sec)<br /><br />mysql&gt; SELECT o.offer_id<br />    -&gt; FROM offer AS o<br />    -&gt; LEFT JOIN clicks AS c<br />    -&gt; ON (o.offer_id = c.offer_id)<br />    -&gt; WHERE c.offer_id IS NULL<br />    -&gt; LIMIT 100,50;<br />50 rows in set (0.04 sec)<br /><br />mysql&gt; SELECT COUNT(o.offer_id)<br />    -&gt; FROM offer AS o<br />    -&gt; LEFT JOIN clicks AS c<br />    -&gt; ON (o.offer_id = c.offer_id)<br />    -&gt; WHERE c.offer_id IS NULL;<br />1 row in set (0.00 sec)<br /></pre><p>Et Voilà! While the SQL_CALC_FOUND_ROWS Queries took more than one minute together, the queries with the second COUNT() query only took a bit more than 30 seconds together, meaning they are twice as fast.</p><p>I'm pretty sure there are situations where SQL_CALC_FOUND_ROWS is the way to go, but in cases like this one you definately wanna go for the COUNT()-Query.</p>
      </div>
    </content>
    	<category term="mysql"/>
    	<category term="performance"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/06/canonicals-what-they-are-and-how-to-use-them.html</id>
    <title>Canonicals: What they are and how to use them</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/06/canonicals-what-they-are-and-how-to-use-them.html"/>
    <updated>2009-06-10T12:37:21Z</updated>
    <published>2009-06-10T12:37:21Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Today I wanna give a short insight on canonicals. A canonical is a (fairly new) SEO item in the head section of a HTML document, which contains the parent URL. Let's say you have a page which contains a sortable list whose URL is <strong>www.chip.de/sortable-list</strong>. Besides AJAX-sorting you also have sorting with URLs like <strong>www.chip.de/sortable-list/order/price/dir/desc</strong>, which reloads the page showing the sortable list ordered by the price descending. Usually you either 2 would have pages in the Google index with the almost the same content or 1 page since you put <em>noindex,nofollow</em> in one of them. This is where canonical comes into play:</p><p>First off you leave the <em>noindex, nofollow</em> in the page with the sorted list. Second you put a canonical into the HTML head section telling search engines where it can find the original list!</p><pre name="code" class="html">&lt;link rel="canonical" href="/sortable-list"&gt;</pre><p>That way the sorted list URL will not only not be in the Google index but the original URL gets more "attention".</p><p>The latest Firefox versions already show if a canonical is defined:</p><p style="text-align: center;"><img title="Canonicals in Firefox" src="images/canonical-firefox.jpg" alt="Canonicals in Firefox" width="546" height="135" /></p><p>P.S. It does no harm to put the canonical tag in the original page itself.</p>
      </div>
    </content>
    	<category term="development"/>
    	<category term="html"/>
    	<category term="seo"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/06/scrumfails-part-2.html</id>
    <title>Scrumfails Part 2</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/06/scrumfails-part-2.html"/>
    <updated>2009-06-10T12:21:42Z</updated>
    <published>2009-06-10T12:21:42Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Sprint Planning I: The product owner presents an item with 8 storypoints. Not one team member can remember to have ever estimated that item and everyone agrees that it's way bigger than an 8. How the hell could that happen? We don't know. What did we do? We excluded the item from the sprint and set up a meeting to discuss the item.</p>
      </div>
    </content>
    	<category term="scrumfails"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/06/job-offer-at-chip-online-as-php-developer.html</id>
    <title>Job offer at CHIP Online as PHP Developer</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/06/job-offer-at-chip-online-as-php-developer.html"/>
    <updated>2009-06-05T10:02:29Z</updated>
    <published>2009-06-05T10:02:29Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>If you speak german and you're looking for a job as PHP Developer, we are looking for one at CHIP Online in the price comparison team.</p><p>In case you are interested, <a href="https://jobs.burda.de/persis/main?fn=bm.jobsdetail&amp;refnr=1986072">click here</a>.</p>
      </div>
    </content>
    	<category term="job offers"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/06/scrumfails-part-1.html</id>
    <title>Scrumfails Part 1</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/06/scrumfails-part-1.html"/>
    <updated>2009-06-04T12:31:24Z</updated>
    <published>2009-06-04T12:31:24Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Estimation meeting. Cards are layed off. One 5, two 8s, one 13. Question from the one with the 5: "Why 8?"</p>
      </div>
    </content>
    	<category term="scrumfails"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/05/cli-catching-ctrlc-kill-commands-and-fatal-errors.html</id>
    <title>CLI: Catching Ctrl+C, kill commands and fatal errors</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/05/cli-catching-ctrlc-kill-commands-and-fatal-errors.html"/>
    <updated>2009-05-31T11:07:06Z</updated>
    <published>2009-05-31T11:07:06Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Sometimes, when you write a Command Line (CLI) script, you want to catch Ctrl+C. Simple Example: Your script does some stuff on the database and you don't want to leave it in the state it had in the moment the script was killed, Ctrl+C'd or quit by a fatal error.</p><p>First off, we have to tell PHP to use ticks, in order for the catches to work (for more information on ticks check the <a href="http://de2.php.net/pcntl_signal">pcntl_signal() documentation</a>):</p><pre name="code" class="php">declare(ticks = 1);<br /></pre><p>Then, we add shutdown functions to the classes that have to rollback the state:</p><pre name="code" class="php">/**<br /> * Cleanup if process has been killed unexpectedly<br /> *<br /> */<br />private function shutdown()<br />{<br />    $this-&gt;getTargetDb()-&gt;rollBack();<br />    print('Script quit unexpectedly. Doing rollback' . PHP_EOL);<br />    exit();<br />}<br /><br />/**<br /> * Method that is executed, when a Fatal Error occurs<br /> *<br /> */<br />public function fatalErrorShutdown()<br />{<br />    $lastError = error_get_last();<br />    if (!is_null($lastError) &amp;&amp; $lastError['type'] === E_ERROR) {<br />        $this-&gt;shutdown();<br />    }<br />}<br /><br />/**<br /> * Method, that is executed, if script has been killed by<br /> * SIGINT: Ctrl+C<br /> * SIGTERM: kill<br /> *<br /> * @param int $signal<br /> *<br />*/<br />public function sigintShutdown($signal)<br />{<br />    if ($signal === SIGINT || $signal === SIGTERM) {<br />        $this-&gt;shutdown();<br />    }<br />}<br /></pre><p>You <strong>shouldn't forget to include an exit() at some point</strong> in the shutdown methods or your script won't quit at all. In the example the shutdown() method (which is called by both shutdown methods, that will be registered afterwards) quits the script as soon as it's done.</p><p>As a last step, you only have to register your shutdown methods and you're done:</p><pre name="code" class="php">$sync = new Process_Sync();<br />// Catch Fatal Error (Rollback)<br />register_shutdown_function(array($sync, 'fatalErrorShutdown'));<br />// Catch Ctrl+C, kill and SIGTERM (Rollback)<br />pcntl_signal(SIGTERM, array($sync, 'sigintShutdown'));<br />pcntl_signal(SIGINT, array($sync, 'sigintShutdown'));<br /></pre>
      </div>
    </content>
    	<category term="cli"/>
    	<category term="development"/>
    	<category term="php"/>
    </entry>
	<entry>
    <id>http://www.phpdevblog.net/2009/05/international-php-conference-spring-edition-day-2.html</id>
    <title>International PHP Conference Spring Edition Day 2</title>
    <link rel="alternate" type="text/html" href="http://www.phpdevblog.net/2009/05/international-php-conference-spring-edition-day-2.html"/>
    <updated>2009-05-27T15:25:48Z</updated>
    <published>2009-05-27T15:25:48Z</published>
    <author>
      <name>Dominik Jungowski</name>
          </author>
    <content type="xhtml" xml:lang="de" xml:base="http://www.phpdevblog.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>Some words on sessions I attended on day 2.</p><h3><strong>OOP &#8230; aber richtig </strong><em>(Stefan Priebsch)</em><br /></h3><p>Some known basics about OOP. It was ok, but it really was nothing new.</p><h3 class="title"><strong>Organized serendipity: Inside report from the core of PHP development</strong> <em>(Lukas Smith)</em></h3><p>Finally I know why the namespace separator discussion took so long ;-)</p><h3 class="title"><strong>Multi-Master MySQL </strong><em>(Arne Blankerts)</em></h3><p>While the session itself was rather disappointing, it got me thinking a lot about how it would be possible to set up a multi master architecture. If I ever have any good idea, I'll let you know ;-)</p>
      </div>
    </content>
    	<category term="conference"/>
    	<category term="php"/>
    </entry>
</feed>