<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>PHP, ZendFramework, MySQL, ExtJS, jQuery, Javascript, ZFS</description><title>jDempster</title><generator>Tumblr (3.0; @jdempster)</generator><link>http://jdempster.com/</link><item><title>Object and Array dereferencing</title><description>&lt;p&gt;There’s been quite alot of talk lately about array dereferencing which I think will be included in php 5.4.&lt;/p&gt;

&lt;p&gt;Here I’ll show you a couple of tricks I use for array and object dereferencing.&lt;/p&gt;

&lt;p&gt;More details on function array dereferencing &lt;a href="https://wiki.php.net/rfc/functionarraydereferencing"&gt;wiki.php.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can already chain methods by returning objects from methods.&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;pre class="brush:php"&gt;
class Foo {
  public function storeMessage($message) {
    $this-&gt;message = $message;
    return $this;
  }

  public function showMessage() {
    reutrn $this-&gt;message;
  } 
}

$foo = new Foo();
echo $foo-&gt;storeMessage('hello world')-&gt;showMessage();
&lt;/pre&gt;

&lt;p&gt;Chaining is great (but don’t get too carried away).
Sometimes we don’t even need to keep the new object we create because we can do everything we want to do with it in a short space of code. Here it becomes handy to be able to redreference the object at construct.&lt;/p&gt;

&lt;pre class="brush:php"&gt;
echo (new Foo())-&gt;storeMessage('hello world')-&gt;showMessage();
&lt;/pre&gt;

&lt;p&gt;Theres nothing wrong with that apart from you need php 5.4 to do it.&lt;/p&gt;

&lt;p&gt;Using the with function work around.&lt;/p&gt;

&lt;pre class="brush:php"&gt;
function with($object) {
  return $object;
}

echo with(new Foo())-&gt;storeMessage('hello world')-&gt;showMessage();
&lt;/pre&gt;

&lt;p&gt;Very short function which just returns the object it was given.&lt;/p&gt;

&lt;p&gt;Array dereferencing is being able to access an element in the array when it’s returned from a method or function.&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;pre class="brush:php"&gt;
function giveMeAnArray() {
   return array('color' =&gt; 'red', 'size' =&gt; 12);
}

echo giveMeAnArray()['color'];
&lt;/pre&gt;

&lt;p&gt;This would produce an error until php 5.4.&lt;/p&gt;

&lt;p&gt;The work around I use.
Example&lt;/p&gt;

&lt;pre class="brush:php"&gt;
function giveMeAnArray() {
   return array('color' =&gt; 'red', 'size' =&gt; 12);
}

echo array_get(giveMeAnArray(), 'color');
&lt;/pre&gt;

&lt;p&gt;Now array_get doesn’t exist in php core. But is a function I find quite handy.&lt;/p&gt;</description><link>http://jdempster.com/post/13248194144</link><guid>http://jdempster.com/post/13248194144</guid><pubDate>Thu, 24 Nov 2011 10:01:05 +0000</pubDate></item><item><title>Constructor Config and array_get</title><description>&lt;p&gt;Some of the functions I use daily don’t exist in the PHP core. But the great thing about programming languages is you can create your own functions.&lt;/p&gt;

&lt;p&gt;Here is handy function I’ve been using alot of lately.&lt;/p&gt;

&lt;p&gt;Originally created by &lt;a href="http://marc.info/?l=php-internals&amp;m=118946242013246&amp;w=2"&gt;Andrew Shearer&lt;/a&gt;&lt;/p&gt;

&lt;pre class="brush:php"&gt;
if (!function_exists('array_get')) {
     function array_get($arr, $key, $default = false) {
         if (array_key_exists($key, $arr)) {
             return $arr[$key];
         }
         else {
             return $default;
         }
     }
}
&lt;/pre&gt;

&lt;p&gt;You might use it in a constructors config array.&lt;/p&gt;

&lt;pre class="brush:php"&gt;
class Foo {
  public function __construct(array $config = array()) {
      $this-&gt;bar = array_get($config, 'foo', 'my default value');
  }

  public function showValue() {
      return $this-&gt;bar;
  }
}

$foo = new Foo();
echo $foo-&gt;showValue(); // outputs "my default value"

$foo = new Foo(array( 'foo' =&gt; 'new value' ));
echo $foo-&gt;showValue(); // outputs "new value"
&lt;/pre&gt;

&lt;p&gt;It can greatly simplfy code.&lt;/p&gt;

&lt;p&gt;As an alterative you could use an config default merge.&lt;/p&gt;

&lt;pre class="brush:php"&gt;
class Foo {
  public function __construct(array $config = array()) {
      $config = $config + array(
          'foo' =&gt; 'my default value'
      );
      $this-&gt;bar = $options['foo'];
  }

  public function showValue() {
      return $this-&gt;bar;
  }
}

$foo = new Foo();
echo $foo-&gt;showValue(); // outputs "my default value"

$foo = new Foo(array( 'foo' =&gt; 'new value' ));
echo $foo-&gt;showValue(); // outputs "new value"
&lt;/pre&gt;</description><link>http://jdempster.com/post/13153364050</link><guid>http://jdempster.com/post/13153364050</guid><pubDate>Tue, 22 Nov 2011 10:00:06 +0000</pubDate></item><item><title>jQuery Flot All Loaded Plugin</title><description>&lt;p&gt;I’ve been using &lt;a href="http://code.google.com/p/flot/"&gt;Flot&lt;/a&gt; a Javascript plotting library for jQuery in a few projects recently. I came across the need to know when all the charts have been drawn.&lt;/p&gt;

&lt;p&gt;Thankfully Flot has nifty support for plugins. So I wrote one. It’s quick and simple.&lt;/p&gt;

&lt;pre class="brush:javascript"&gt;
    (function ($) {
        // charts will store how many charts initialised, drawn will store how many have been drawn.
        var charts = 0, drawn = 0;

        function monitor() {
            // if we have more charts than there are charts drawn, check again in a little while
            if (charts &gt; drawn) {
                setTimeout(monitor, 50);
            } else {
                // all charts have finished being drawn, if theres a function call it
                if (window.flotCompleted instanceof 'function') {
                    window.flotCompleted();
                }
            }
        }

        // init is called for every flot chart
        function init(plot) {
            // we have another chart
            charts++;
            plot.hooks.draw.push(function() {
                // a chart has finished being drawn
                drawn++;
            });
        }

        // register the plugin
        $.plot.plugins.push({
            init: init,
            name: 'loadingmonitor'
        });

        // give the browser time to have all the charts registered
        setTimeout(monitor, 200);
    })(jQuery);
&lt;/pre&gt;</description><link>http://jdempster.com/post/11396161666</link><guid>http://jdempster.com/post/11396161666</guid><pubDate>Thu, 13 Oct 2011 16:10:06 +0100</pubDate><category>flot</category><category>jquery</category><category>plugin</category><category>javascript</category></item><item><title>Calculating MySQL table sizes</title><description>&lt;p&gt;Sometimes we need to know how much disk-space a table is using. Or which table is using the most disk-space.
Here’s a handy query.&lt;/p&gt;

&lt;p&gt;Here’s a handy query which uses the &lt;a href="http://dev.mysql.com/doc/refman/5.1/en/information-schema.html"&gt;information_schema&lt;/a&gt; tables MySQL provides.&lt;/p&gt;

&lt;p&gt;It can take MySQL sometime to build the information up required to produce the results.&lt;/p&gt;

&lt;pre class="brush:sql"&gt;
SELECT
  CONCAT(table_schema, '.', table_name) AS table_name,
  CONCAT(ROUND(data_length / (1024 *1024), 2) , 'M') AS data_length,
  CONCAT(ROUND(index_length / (1024 *1024), 2) , 'M') AS index_length,
  CONCAT(ROUND((data_length + index_length ) / (1024 *1024), 2), 'M') AS total_size,
  CURRENT_DATE AS last_update

FROM information_schema.tables
WHERE table_schema = 'database_name'
ORDER BY ROUND((data_length + index_length) / (1024 *1024), 2) DESC;
&lt;/pre&gt;

&lt;p&gt;Note; Deleting tables and getting a smaller size from the query doesn’t necessarily mean that the disk space will be freed. InnoDB keeps the disk space used and marks as available in it’s table-space.&lt;/p&gt;</description><link>http://jdempster.com/post/11097752337</link><guid>http://jdempster.com/post/11097752337</guid><pubDate>Thu, 06 Oct 2011 12:35:06 +0100</pubDate><category>mysql</category></item><item><title>Warming/Loading tables into MySQL Cache</title><description>&lt;p&gt;After restarting MySQL you’re left with a cold machine. No cache.
Putting this directly back into use can leave it struggling to load data from disk again.&lt;/p&gt;

&lt;p&gt;One way of helping out is to give it some time to load table data back into that vast amount of RAM it has.&lt;/p&gt;

&lt;p&gt;One way I’ve found that’s helped me is to create a temporary table, alter it’s engine into a black hole then select everything from an existing table into it.&lt;/p&gt;

&lt;pre class="brush:sql"&gt;
CREATE TEMPORARY TABLE blackholeMyTable ENGINE=BLACKHOLE SELECT * FROM MyTable;
&lt;/pre&gt;

&lt;p&gt;This cause MySQL to go though the works of loading the data from disk to insert into a blackhole. Thus being put into RAM.&lt;/p&gt;</description><link>http://jdempster.com/post/10762810427</link><guid>http://jdempster.com/post/10762810427</guid><pubDate>Wed, 28 Sep 2011 10:56:05 +0100</pubDate><category>mysql</category></item><item><title>PHP Tail File</title><description>&lt;h3&gt;Why tail a file?&lt;/h3&gt;

&lt;p&gt;Sometimes there’s a need to tail log files. This might be to store data in a more structured form, say in a database. Maybe you need to tail al log file to monitor for a certain condition. These log files could be anything, like maybe Apache or Mail logs.&lt;/p&gt;

&lt;p&gt;PHP doesn’t come with any easy way built in to tail a file. Unix comes with the handle tail tool. I use tail regularly mixed with a grep, looking for a certain entry which I know will appear.&lt;/p&gt;

&lt;pre class="brush:bash"&gt;
// tail the apache log only showing line containing the local IP.
$ tail -f /var/log/apache.log | grep 127.0.01
&lt;/pre&gt;

&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre class="brush:php"&gt;
// create tail object with a file containing our log data
$tail = new FileTail('/var/log/apache.log');

// if we have a previous position we ran up to restore that position
if (is_file('pos')) {
    $tail-&gt;setPos(file_get_contents('pos'));
}

// while we can read aline
while ($line = $tail-&gt;readLine()) {
    // store where we've got to
    file_put_contents('pos', $tail-&gt;getPos());

    // use a regular expression to read values out of the line
    ....
    // use a database connection to store 
    ....
}
&lt;/pre&gt;

&lt;p&gt;This code would run until stopped. With blocking turned on, TailFile will wait and keep checking for newly appended lines.&lt;/p&gt;

&lt;h3&gt;Source Code&lt;/h3&gt;

&lt;script src="https://gist.github.com/1245492.js?file=FileTail.php"&gt;&lt;/script&gt;</description><link>http://jdempster.com/post/10733096369</link><guid>http://jdempster.com/post/10733096369</guid><pubDate>Tue, 27 Sep 2011 19:35:08 +0100</pubDate><category>php</category><category>tail</category><category>file</category></item><item><title>Facebook’s “phpsh” - interactive shell for PHP</title><description>&lt;a href="http://everzet.com/post/1007878331/facebooks-phpsh-interactive-shell-for-php"&gt;Facebook’s “phpsh” - interactive shell for PHP&lt;/a&gt;: &lt;p&gt;One of the best PHP shells I’ve seen. Made by the development team at Facebook.&lt;/p&gt;

&lt;p&gt;Unfortunately mostly written in python, but thats available on most systems.&lt;/p&gt;</description><link>http://jdempster.com/post/7302846987</link><guid>http://jdempster.com/post/7302846987</guid><pubDate>Wed, 06 Jul 2011 15:44:18 +0100</pubDate><category>php</category><category>shell</category></item><item><title>PHP unix man pages</title><description>&lt;p&gt;A recent post from php.net explains a tool I didn’t know existed &lt;strong&gt;pman&lt;/strong&gt;. Unix man pages of PHP functions.&lt;/p&gt;

&lt;p&gt;This little tool makes accessing manual pages for PHP functions incredibility fast.&lt;/p&gt;

&lt;p&gt;Installation is very easy:&lt;/p&gt;

&lt;pre class="brush:bash"&gt;
    # using pear to install the tool
    $ pear install doc.php.net/pman

    # use the command pman followed by the function name
    $ pman strlen
&lt;/pre&gt;

&lt;p&gt;This example instantly displays a unix style man page loaded locally of the strlen docs.&lt;/p&gt;

&lt;p&gt;This is a great way of keeping a local copy of PHP docs for those times when you can’t access php.net.&lt;/p&gt;

&lt;p&gt;I also find the unix style man docs easier to read. After all you only need to get used to reading one style of manual page (although php.net manual style has been some of the best I’ve come across).&lt;/p&gt;</description><link>http://jdempster.com/post/6986327983</link><guid>http://jdempster.com/post/6986327983</guid><pubDate>Mon, 04 Jul 2011 17:24:43 +0100</pubDate><category>php</category><category>manual</category></item><item><title>Using strace to debug PHP</title><description>&lt;h3&gt;The Problem&lt;/h3&gt;

&lt;p&gt;Recently I stumbled into an issue with the PHP FTP functions. After successfully connecting to a server and authenticating, fetching a file list using &lt;a href="http://php.net/manual/en/function.ftp-nlist.php"&gt;ftp_nlist&lt;/a&gt; would return NULL. This isn’t even a return type listed in the manual.&lt;/p&gt;

&lt;h4&gt;Example&lt;/h4&gt;

&lt;pre class="brush:php; gutter:true"&gt;
$ftp_server = 'ftp.example.com';

// set up basic connection
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server\n");

$ftp_user_name = 'user';
$ftp_user_pass = 'pass';

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("Login failed");

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");

// output $contents
var_dump($contents);
&lt;/pre&gt;

&lt;p&gt;Why was it failing? It was connecting fine, there where no problems with authenticating.&lt;/p&gt;

&lt;p&gt;As a developer getting into this situation is very frustrating. Where do we turn to?&lt;/p&gt;

&lt;h3&gt;The Solution (strace)&lt;/h3&gt;

&lt;h4&gt;What is strace&lt;/h4&gt;

&lt;p&gt;One solution is &lt;a href="http://linux.die.net/man/1/strace"&gt;strace&lt;/a&gt; (presumably short for “system trace”), a system debugging tool for linux. There are other Unix variants like dtruss and dtrace. In this article we’ll concentrate on strace.&lt;/p&gt;

&lt;p&gt;The tool traces system calls and signals made by the process you tell it to trace. The name of each system call, its arguments and its return value are printed on standard error, which isn’t pretty. I find it helpful to output the results to a file (-o filename argument to strace can help), alternatively you can redirect standard error to standard out and pipe the results though grep.&lt;/p&gt;

&lt;h4&gt;How to use strace&lt;/h4&gt;

&lt;p&gt;There are two ways you can tell it to trace a process, which depends on if the process is already running or not.&lt;/p&gt;

&lt;h6&gt;Trace an already running process&lt;/h6&gt;

&lt;p&gt;Attach to the process with the process ID pid and begin tracing. The trace can be terminated at any time by a keyboard interrupt signal (ctrl-c). strace will detach itself from the process leaving it to continue running. This can be handy to look at what a long running script is currently doing. This includes things like Apache.&lt;/p&gt;

&lt;pre class="brush:bash"&gt;
// -p pid
$ strace -p 123
&lt;/pre&gt;

&lt;h6&gt;Trace a specified command&lt;/h6&gt;

&lt;p&gt;The other way we can trace something is by telling it the command we want executed and traced. Anything which is not one of its own arguments is the command it will execute, anything after that are arguments to the executing command.&lt;/p&gt;

&lt;pre class="brush:bash"&gt;
// -e is an argument of strace with the value of open
// -v is an argument of php with no value
$ strace -e open php -v 2&gt;&amp;1 | grep php.ini
// outputs the location of the php.ini file which was loaded
&lt;/pre&gt;

&lt;h6&gt;Understanding the output&lt;/h6&gt;

&lt;p&gt;Running the command:&lt;/p&gt;

&lt;pre class="brush:c"&gt;
strace -e open php -v 2&gt;&amp;1 | grep php.ini
&lt;/pre&gt;

&lt;p&gt;Outputs:&lt;/p&gt;

&lt;pre class="brush:c"&gt;
open("/usr/bin/php.ini", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
open("/etc/php5/cli/php.ini", O_RDONLY|O_LARGEFILE) = 3
&lt;/pre&gt;

&lt;p&gt;Here we see that PHP attempted to open two files. One in directory where the binary is, and one in /etc/php5/cli/ this is where PHP has been compiled to look. The first system call failed with No such file or directory.&lt;/p&gt;

&lt;p&gt;You can learn more about system calls and their return values from the linux documentation.
&lt;a href="http://linux-documentation.com/en/man/man2/open.html"&gt;&lt;a href="http://linux-documentation.com/en/man/man2/open.html"&gt;http://linux-documentation.com/en/man/man2/open.html&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Strace &amp; PHP Continued…&lt;/h3&gt;

&lt;h4&gt;Find php.ini&lt;/h4&gt;

&lt;p&gt;Example Command:&lt;/p&gt;

&lt;pre class="brush:bash"&gt;
// -e open, trace only the open system call
// -v ask php to show it's version
// 2&gt;&amp;1 redirect stderr to stdout
// | grep php.ini filter the output for the string php.ini
$ strace -e open php -v 2&gt;&amp;1 | grep php.ini
// outputs the location of the php.ini file which was loaded :)
&lt;/pre&gt;

&lt;p&gt;Example Output:&lt;/p&gt;

&lt;pre class="brush:c"&gt;
open("/usr/bin/php.ini", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
open("/etc/php5/cli/php.ini", O_RDONLY|O_LARGEFILE) = 3
&lt;/pre&gt;

&lt;p&gt;php.ini is being loaded from /etc/php5/cli/php.ini&lt;/p&gt;

&lt;h4&gt;File not loading&lt;/h4&gt;

&lt;p&gt;Example Command:&lt;/p&gt;

&lt;pre class="brush:bash"&gt;
strace -e open php test.php 2&gt;&amp;1 | grep include.php
&lt;/pre&gt;

&lt;p&gt;Example Output:&lt;/p&gt;

&lt;pre class="brush:c"&gt;
open("/usr/bin/include.php", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
&lt;/pre&gt;

&lt;p&gt;Oops maybe we have the wrong path to include the file.&lt;/p&gt;

&lt;h3&gt;Fixing the problem&lt;/h3&gt;

&lt;pre class="brush:bash"&gt;
$ strace -s 300 -e connect,send,recv php ftpTest.php
&lt;/pre&gt;

&lt;ul&gt;&lt;li&gt;-s 300 (Specify the maximum string size to print the default is 32)&lt;/li&gt;
&lt;li&gt;-e connect,send,recv (Trace only the specified set of system calls)&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The output from this strace showed that ftp_nlist was asking the remote server to connect in on a local IP. Clearly an internet ftp server can’t connect in to our local ip. PHP’s error handling on this is clearly insufficient, but thanks to the handy functionality of strace was easy to find. In this case the fix was to make the connection pasv.&lt;/p&gt;

&lt;h3&gt;Helpful links&lt;/h3&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Strace"&gt;&lt;a href="http://en.wikipedia.org/wiki/Strace"&gt;http://en.wikipedia.org/wiki/Strace&lt;/a&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://linux.die.net/man/1/strace"&gt;&lt;a href="http://linux.die.net/man/1/strace"&gt;http://linux.die.net/man/1/strace&lt;/a&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description><link>http://jdempster.com/post/7121372019</link><guid>http://jdempster.com/post/7121372019</guid><pubDate>Mon, 04 Jul 2011 17:15:24 +0100</pubDate><category>php</category><category>strace</category><category>linux</category><category>debug</category></item></channel></rss>

