Permalink
Browse files

Initial commit

  • Loading branch information...
salathe committed Jan 24, 2014
0 parents commit 3326c3a5a71ff876c95ce97e24fddfb8d004327b
Showing with 2,108 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +10 −0 404.html
  3. +1 −0 CNAME
  4. +1 −0 LICENSE.md
  5. +2 −0 README.md
  6. +20 −0 _config.yml
  7. +30 −0 _includes/head.html
  8. +44 −0 _layouts/default.html
  9. +8 −0 _layouts/page.html
  10. +16 −0 _layouts/post.html
  11. +27 −0 _posts/2007-03-15-select-top-parameter.markdown
  12. +55 −0 _posts/2007-09-17-php-dont-repeat-yourself-when-using-printf.markdown
  13. +134 −0 _posts/2007-09-19-php5-method-chaining.markdown
  14. +38 −0 _posts/2008-01-12-php-vars-curly-braces.markdown
  15. +69 −0 _posts/2008-01-13-get-file-extension-comparison.markdown
  16. +48 −0 _posts/2008-01-22-imagecreatefrom-string-remote.markdown
  17. +13 −0 _posts/2008-02-29-elephpant.markdown
  18. +85 −0 _posts/2008-11-29-download-speed-php-curl.markdown
  19. +32 −0 _posts/2009-03-15-tiny-little-gifs.markdown
  20. +50 −0 _posts/2009-04-01-php-array-mode.markdown
  21. +6 −0 _posts/2009-06-12-php-doc.markdown
  22. +45 −0 _posts/2009-06-14-parsing-dates-with-datetime.markdown
  23. +30 −0 _posts/2009-06-15-xpath-css-class.markdown
  24. +128 −0 _posts/2009-07-08-anonymous-closures.markdown
  25. +43 −0 _posts/2009-09-25-infiniteiterator.markdown
  26. +177 −0 _posts/2009-10-23-php-funcs-xpath.markdown
  27. +189 −0 _posts/2010-04-30-glob-patterns.markdown
  28. +58 −0 _posts/2012-03-17-flattening-a-multidimensional-array-in-php.markdown
  29. +28 −0 atom.xml
  30. +20 −0 index.html
  31. BIN public/apple-touch-icon-144x144-precomposed.png
  32. +403 −0 public/css/poole.css
  33. +194 −0 public/css/style.css
  34. +74 −0 public/css/syntax.css
  35. +29 −0 public/dude-web.svg
  36. BIN public/favicon.ico
@@ -0,0 +1 @@
+_site
@@ -0,0 +1,10 @@
+---
+layout: default
+title: "404: Page not found"
+permalink: /404.html
+---
+
+<div class="page">
+ <h1 class="page-title">404: Page not found</h1>
+ <p class="lead">Sorry, we've misplaced that URL or it's pointing to something that doesn't exist. <a href="/">Head back home</a> to try finding it again.</p>
+</div>
1 CNAME
@@ -0,0 +1 @@
+www.cowburn.info
@@ -0,0 +1 @@
+Copyright (c) 2014 Peter Cowburn
@@ -0,0 +1,2 @@
+
+My blog. http://cowburn.info
@@ -0,0 +1,20 @@
+# Dependencies
+markdown: redcarpet
+pygments: true
+
+# Markdown
+redcarpet:
+ extensions: ["no_intra_emphasis", "fenced_code_blocks", "autolink", "strikethrough", "superscript", "with_toc_data"]
+
+
+# Permalinks
+permalink: /:year/:month/:day/:title/index.html
+
+title: Salathe
+
+author:
+ name: 'Peter Cowburn'
+ url: http://cowburn.info
+
+github:
+ repo: https://github.com/salathe/cowburn.info/
@@ -0,0 +1,30 @@
+<head>
+ <link href="http://gmpg.org/xfn/11" rel="profile">
+ <meta http-equiv="content-type" content="text/html; charset=utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
+
+ <title>
+ {% if page.title %}
+ {{ page.title }} &middot; {{ site.title }}
+ {% else %}
+ {{ site.title }}
+ {% endif %}
+ </title>
+
+ <!-- CSS -->
+ <link rel="stylesheet" href="/public/css/poole.css">
+ <link rel="stylesheet" href="/public/css/syntax.css">
+ <link rel="stylesheet" href="/public/css/style.css">
+ <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Droid+Sans+Mono|PT+Mono|PT+Serif:400,400italic,700|PT+Sans:400">
+
+ <!-- JS -->
+ <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
+ <script src="//cdnjs.cloudflare.com/ajax/libs/waypoints/2.0.3/waypoints.min.js"></script>
+
+ <!-- Icons -->
+ <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/public/apple-touch-icon-144x144-precomposed.png">
+ <link rel="shortcut icon" href="/public/favicon.ico">
+
+ <!-- RSS -->
+ <link rel="alternate" type="application/rss+xml" title="RSS" href="/atom.xml">
+</head>
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">
+
+ {% include head.html %}
+
+ <body>
+
+ <header id="logobit">
+ <div class="container">
+ <div class="logo">
+ <a href="/" title="Go to Homepage"></a>
+ </div>
+ </div>
+ </header>
+
+ <div class="masthead">
+ <div class="container">
+ <h3 class="masthead-title">
+ <a href="/" title="Home">{{ site.title }}</a>
+ <small>— Find me on
+ <a href="https://github.com/salathe">GitHub</a> and
+ <a href="http://stackoverflow.com/users/113938/salathe">StackOverflow</a>.
+ </small>
+ </h3>
+ </div>
+ </div>
+
+ <div class="container content">
+ {{ content }}
+ </div>
+
+ <script>
+ var $target = $('#logobit');
+ $('div.container.content').waypoint(function(direction) {
+ if (direction == 'up') {
+ $target.removeClass('scroll');
+ } else {
+ $target.addClass('scroll');
+ }
+ });
+ </script>
+
+ </body>
+</html>
@@ -0,0 +1,8 @@
+---
+layout: default
+---
+
+<div class="page">
+ <h1 class="page-title">{{ page.title }}</h1>
+ {{ content }}
+</div>
@@ -0,0 +1,16 @@
+---
+layout: default
+---
+
+<div class="post">
+ <h1 class="post-title">{{ page.title }}</h1>
+ <span class="post-date">{{ page.date | date_to_string }}</span>
+ {{ content }}
+</div>
+
+<footer>
+ <p class="message">
+ If you liked this article, you may want to
+ <a href="/">browse my other articles</a>.
+ </p>
+</footer>
@@ -0,0 +1,27 @@
+---
+layout: post
+title: T-SQL - Using a parameter in SELECT TOP clause
+excerpt: Wrap the variable in parenthesis and magic...
+---
+I've been working with Stored Procedures a lot recently, even though the whole idea was new to be before starting this job. Today I was trying to select the first <code>n</code> rows from a table, but wanted to be able to change <code>n</code> via a parameter in the procedure call. I thought something like the following would work, but it throws an error when trying to use the variable for the TOP clause.
+
+<pre lang="sql">CREATE PROCEDURE dbo.sp_TestGetAll
+ @Limit int
+AS
+SELECT TOP @Limit
+ id, col1, col2, col3
+FROM TestTable</pre>
+
+After a brief Google search there were a few methods presented including such nasties as manually writing out a SQL string within the procedure and executing that instead! Also, it was suggested to change the ROWCOUNT variable before and after issuing the select. Sheesh, that's messy.
+
+Well anyway, the solution which works for me and is much easier is simply to wrap the variable in parenthesis and magic... it works.
+
+<pre lang="sql">CREATE PROCEDURE dbo.sp_TestGetAll
+ @Limit int
+AS
+-- Notice the parentheses around @Limit - that is the only change!
+SELECT TOP (@Limit)
+ id, col1, col2, col3
+FROM TestTable</pre>
+
+I'm just really noting this down so that I don't forget, do another Google search and then resort to those other ugly ways of doing this simple task.
@@ -0,0 +1,55 @@
+---
+layout: post
+title: Don't repeat yourself when using printf!
+excerpt: Using numbered placeholders to reuse argument values.
+---
+Hi folks,
+
+We all use <a href="http://php.net/printf">printf</a> (or <a href="http://php.net/sprintf">sprintf</a>) to help ourselves when mingling together output strings with our variables, right? This is a tip that I use often, but I continually see people writing code where this technique would be useful but isn't used. What _am_ I on about? <a id="more"></a><a id="more-14"></a> Well, how many times have you seen people write something similar to this:
+<pre lang="php">echo '<ul>';
+foreach ($aItems as $aItem)
+{
+ // <li><a href="link.html" title="Link 2">Link 2</a> (link.html)</li>
+ printf('<li><a href="%s" title="%s">%s</a> (%s)</li>',
+ $aItem['link'],
+ $aItem['title'],
+ $aItem['title'],
+ $aItem['link']);
+}
+echo '</ul>';</pre>
+
+Notice that in the <code>printf</code> call, there are a number of repeated arguments (2 x <code>$aItem['link']</code>, and 2 x <code>$aItem['title']</code>). Wouldn't it be great if we could satisfy the lazy side within all of us and not repeat ourselves? Well, it's possible! Take a look at the amended example below:
+<pre lang="php">printf('<li><a href="%1$s" title="%2$s">%2$s</a> (%1$s)</li>',
+ $aItem['link'],
+ $aItem['title']);</pre>
+
+Ok, I admit for those of you not well versed in using this (or those who didn't look at the code very hard) might well be a little lost right now. What's with the extra stuff messing up our nice, normal "%s" placeholders?
+
+Well, the extra stuff is what helps us to be lazy! The "1$" and "2$" allow us to point to the argument number which will fill in that particular placeholder. In the code above, 1$ refers to <code>$aItem['link']</code> and 2$ refers to <code>$aItem['title']</code>. The numbers preceding the dollar sign simply reference the order of the arguments fed into the function call. What makes things useful for us, in this instance, is that the numbered replacements can be repeated any number of times within the template string without the need to add more (of the same) arguments as we did in the original example up above!
+
+Finally, here is another example which hopefully should help to cement things into your own mind about using numbered placeholders in your (s)printf function calls:
+<pre lang="php">$szAdjective = 'fluffy';
+$szNoun = 'cat';
+
+// Both methods will output the following:
+// Yesterday, I saw a cat. It was a fluffy cat!
+// I have never seen a cat quite so fluffy.
+
+// Old-school method
+printf('Yesterday, I saw a %s. '.
+ 'It was a %s %s! I have '.
+ 'never seen a %s quite so %s.',
+ $szNoun,
+ $szAdjective,
+ $szNoun,
+ $szNoun,
+ $szAdjective);
+
+// Sexy, lazy method
+printf('Yesterday, I saw a %1$s. '.
+ 'It was a %2$s %1$s! I have '.
+ 'never seen a %1$s quite so %2$s.',
+ $szNoun,
+ $szAdjective);</pre>
+
+Be lazy, use numbered placeholders! :)
@@ -0,0 +1,134 @@
+---
+layout: post
+title: PHP5 Method Chaining
+excerpt: If you <code>return $this</code> then you can chain method calls together.
+---
+<strong>Introduction</strong>
+
+In this article, I'll be talking about a useful new feature introduced in PHP5 as part of the OOP improvements over PHP4. This feature is called Method Chaining and enables us to do pretty cool things like:
+
+<pre lang="php">
+$object->method_a()->method_b()->method_c();
+</pre><a id="more"></a><a id="more-15"></a>
+
+So really what's this all about? Well, one change (of many) between PHP4 and PHP5 is that now methods can return <code>objects</code>. That's only a small change but allows for a new way of thinking about how you handle objects and their methods.
+
+In order to be able to chain methods together, as mentioned earlier, you need to be able to return an object. The returned object does not necessarily need be the same one from which the method is being called, but we'll not be touching on that fact in this article. Let's go for a quick illustration of how to put all of this into action.
+
+<strong>Regular Use of a Class</strong>
+We'll start off with a basic class, which doesn't do anything particularly interesting but it will serve our purpose.
+
+<pre lang="php">
+// This Person class encapsulates a couple of properties which
+// a person might have: their name and age.
+// We also give the Person the opportunity to introduce themselves.
+class Person
+{
+ private $m_szName;
+ private $m_iAge;
+
+ public function setName($szName)
+ {
+ $this->m_szName = $szName;
+ }
+
+ public function setAge($iAge)
+ {
+ $this->m_iAge = $iAge;
+ }
+
+ public function introduce()
+ {
+ printf(
+ 'Hello my name is %s and I am %d years old.',
+ $this->m_szName,
+ $this->m_iAge);
+ }
+}
+</pre>
+
+Now, we can create a new <code>Person</code> (see example below) and give them a name and age. Using the <code>introduce</code> method, the person can them introduce themselves.
+
+<pre lang="php">
+// We'll be creating me, digitally.
+$peter = new Person();
+
+// Let's set some attributes and let me introduce myself.
+$peter->setName('Peter');
+$peter->setAge(23);
+$peter->introduce();
+</pre>
+
+The above example would output:
+<blockquote><code>Hello my name is Peter and I am 23 years old.</code></blockquote>
+
+
+<strong>Implementing Method Chaining</strong>
+Great, but you already knew how to do that didn't you! In actuality, we only need to add two lines of code to the basic class above to enable us to write a chain, and the second line is a repeat of the first! How's it done? Quite simply we need to return the <code>Person</code> object from each method. At the moment, none of the methods actually return anything so we'll amend that now. We just need to add <code>return $this;</code> to the methods <code>setName</code> and <code>setAge</code>.
+
+The amended <code>Person</code> class is as follows:
+<pre lang="php">
+// This Person class encapsulates a couple of properties which
+// a person might have: their name and age.
+// We also give the Person the opportunity to introduce themselves.
+class Person
+{
+ private $m_szName;
+ private $m_iAge;
+
+ public function setName($szName)
+ {
+ $this->m_szName = $szName;
+ return $this; // We now return $this (the Person)
+ }
+
+ public function setAge($iAge)
+ {
+ $this->m_iAge = $iAge;
+ return $this; // Again, return our Person
+ }
+
+ public function introduce()
+ {
+ printf(
+ 'Hello my name is %s and I am %d years old.',
+ $this->m_szName,
+ $this->m_iAge);
+ }
+}
+</pre>
+
+Ok, is that it? Yep, really. Because we return the <code>Person</code> from those methods, it enables us to group method calls together in a sequence, or chain. I'll re-do the previous example but this time using our new magical chaining abilities.
+
+<pre lang="php">
+// We'll be creating me, digitally.
+$peter = new Person();
+
+// Let's set some attributes and let me introduce myself,
+// all in one line of code.
+$peter->setName('Peter')->setAge(23)->introduce();
+</pre>
+
+That example will produce exactly the same output as our regular, boring example earlier! All because we return the <code>Person</code> object in the <code>setName/Age</code> methods.
+
+<strong>How does that work?</strong>
+If you're a bit confused about precisely what is going on here, let me try to walk you through the process, step by step. We'll go through the line of code as it is processed, from left to right.
+
+First up is <code>$peter->setName('Peter')</code>. This assigns the person's name to be Peter, my name and returns <code>$this</code> -- that is, the <code>$peter</code> object. So at the moment Peter has his name, but no age!
+
+Next up comes <code>->setAge(23)</code>. Because we're chained with the previous method, PHP interprets the code and says "execute the <code>setAge</code> method belonging to whatever was returned from the previous method." In this case, PHP executes the <code>setAge</code> method belonging to our <code>Person</code> object, <code>$peter</code>.
+
+The exact same thing happens when we finally call the <code>introduce</code> method. PHP executes the <code>introduce</code> method belonging to whatever was returned from the <code>setAge</code> method call: <code>$peter</code>.
+
+Hopefully, that's at least a bit clear and I'll let you mull over this confusing (until you <em>get</em> it) subject for a while. I'll leave you with the note that because we can chain methods, they can be called more than once and in whatever order we like which could lead to the amazingly useless snippet below:
+<pre lang="php">
+// Hello my name is Winifred and I am 72 years old.
+$peter->setAge(23)
+ ->setName('Peter')
+ ->setName('Winifred')
+ ->setAge(72)
+ ->introduce();
+</pre>
+
+<strong>In Conclusion</strong>
+We can boil this whole article down into one short sentence. If you <code>return $this</code> then you can chain the method calls together. Done. :)
Oops, something went wrong.

0 comments on commit 3326c3a

Please sign in to comment.