<?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>Inspire Labs &#187; PHP</title>
	<atom:link href="http://www.inspirelabs.co.uk/category/development/backend/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.inspirelabs.co.uk</link>
	<description>Updates from Inspire Digital at IPC Media</description>
	<lastBuildDate>Wed, 05 Oct 2011 14:03:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>BetterXML</title>
		<link>http://www.inspirelabs.co.uk/05/10/2011/betterxml/</link>
		<comments>http://www.inspirelabs.co.uk/05/10/2011/betterxml/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 14:03:49 +0000</pubDate>
		<dc:creator>John Wright</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.inspirelabs.co.uk/?p=345</guid>
		<description><![CDATA[Everyone hates the way that PHP&#8217;s SimpleXML object handles variables. We find ourselves constantly checking if the string we&#8217;ve been given back contains numeric or boolean values. Well&#8230; here&#8217;s a quick fix. &#38;lt;?php &#160; /** * A decorator for SimpleXML to add 2 important things: * 1) The &#34;hasChildren() method&#34;. * 2) The &#34;asVar()&#34; method [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone hates the way that PHP&#8217;s SimpleXML object handles variables. We find ourselves constantly checking if the string we&#8217;ve been given back contains numeric or boolean values. Well&#8230; here&#8217;s a quick fix.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?php
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A decorator for SimpleXML to add 2 important things:
 * 1) The &quot;hasChildren() method&quot;.
 * 2) The &quot;asVar()&quot; method which uses type checking to discover integers and boolean values.
 *
 * @author John Wright
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> BetterXML implements ArrayAccess<span style="color: #339933;">,</span> Iterator
<span style="color: #009900;">&#123;</span>
  <span style="color: #009933; font-style: italic;">/**
   * The SimpleXML object
   *
   * @var SimpleXMLElement
   */</span>
  protected <span style="color: #000088;">$simple_xml</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Integer value used for iteration.
   *
   * @var integer
   */</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * When methods don't exist in this class, check the decorated
   * SimpleXMLElement object.
   *
   * @param string $method
   * @param mixed[] $args
   * @return mixed
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span> <span style="color: #000088;">$args</span><span style="color: #339933;">=</span><span style="color: #990000;">array</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    throw <span style="color: #000000; font-weight: bold;">new</span> BadMethodCallException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Method BetterXML::<span style="color: #006699; font-weight: bold;">$method</span>() does not exist.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Constructor
   *
   * @param string|SimpleXMLElement $content This can be a string containing XML content, a string representing a path to an XML file or a SimpleXML object
   * @return BetterXML
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span> instanceof SimpleXMLElement<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml <span style="color: #339933;">=</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</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;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</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;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Decorate all SimpleXML objects with the BetterXML class.
   *
   * @param string $var_name
   * @return BetterXML
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var_name</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$var_name</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</span> ? <span style="color: #000000; font-weight: bold;">new</span> BetterXML<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Setting will actually set to the decorated SimpleXML object.
   *
   * @var string $name
   * @var mixed $value
   * @return void
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Decoration method for SimpleXML::_toString().
   *
   * @return string
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Returns a variable other than BetterXML.
   *
   * If this has children, the method will just return itself.
   * If the content is numeric, the method will return an integer or float.
   * If the content is either &quot;true&quot; or &quot;false&quot; the method will return a boolean.
   * If all else fails, this will return the content as a string.
   *
   * @return mixed
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> asVar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>hasChildren<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: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<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: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span> ? <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$var</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^true$/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^false$/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Returns the decorated SimpleXML object.
   *
   * @return SimpleXMLElement
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getRaw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Returns a boolean depicting whether this element has
   * children.
   *
   * @return boolean
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> hasChildren<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>children<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span> ? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**#@+
   * @see Iterator
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>offsetGet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>pos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**#@-*/</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>pos<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**#@-*/</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">++</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>pos<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>offsetGet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>pos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**#@-*/</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">rewind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>pos <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>offsetGet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>pos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**#@-*/</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> valid<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>offsetExists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>pos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**#@+
   * @see ArrayAccess
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> offsetExists<span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #009900;">&#91;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**#@-*/</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> offsetGet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #009900;">&#91;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</span> ? <span style="color: #000000; font-weight: bold;">new</span> BetterXML<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$var</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**#@-*/</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> offsetSet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #009900;">&#91;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**#@-*/</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> offsetUnset<span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>simple_xml<span style="color: #009900;">&#91;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#93;</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>

]]></content:encoded>
			<wfw:commentRss>http://www.inspirelabs.co.uk/05/10/2011/betterxml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>YAML Reference Card</title>
		<link>http://www.inspirelabs.co.uk/14/08/2009/yaml-reference-card/</link>
		<comments>http://www.inspirelabs.co.uk/14/08/2009/yaml-reference-card/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 12:49:04 +0000</pubDate>
		<dc:creator>John Wright</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://inspirelabs.co.uk/?p=150</guid>
		<description><![CDATA[Our projects contain a lot of configuration. So much, in fact, that our parsers have been struggling with the size of our files and causing sites to time out :s. Therefore, the exemplar team have created a reference YAML. We write some default configurations in our reference YAML and then our main configuration can use [...]]]></description>
			<content:encoded><![CDATA[<p>Our projects contain a lot of configuration. So much, in fact, that our parsers have been struggling with the size of our files and causing sites to time out :s. Therefore, the exemplar team have created a reference YAML. We write some default configurations in our reference YAML and then our main configuration can use these defaults and override them.</p>
<p>This has significantly improved our performance for Exemplar. However, just recently I came across this page: <a href="http://www.yaml.org/refcard.html">yaml.org/refcard.html</a> which pretty much said what we were trying to achieve is (or should be) already available in our YAML parsing. I thought I&#8217;d share my findings with you as a lot of people don&#8217;t seem to know of these features.</p>
<h2>Aliasing</h2>
<p>here&#8217;s an example of repetitiveness in YAMLs:</p>
<pre><span style="color: #0000ff;">bill-to</span>:
  <span style="color: #0000ff;">given</span>: Chris
  <span style="color: #0000ff;">family</span>: Dumars
  <span style="color: #0000ff;">address</span>:
    <span style="color: #0000ff;">lines</span>: |
<span style="color: #999999;">      458 Walman Dr.
      Suite #292</span>
    <span style="color: #0000ff;">city</span>: Royal Oak
    <span style="color: #0000ff;">state</span>: MI
    <span style="color: #0000ff;">postal</span>: <span style="color: #ff6600;">48046</span>
<span style="color: #0000ff;">ship-to</span>:
  <span style="color: #0000ff;">given</span>: Chris
  <span style="color: #0000ff;">family</span>: Dumars
  <span style="color: #0000ff;">address</span>:
    <span style="color: #0000ff;">lines</span>: |
 <span style="color: #999999;">     458 Walman Dr.
      Suite #292</span>
    <span style="color: #0000ff;">city</span>: Royal Oak
    <span style="color: #0000ff;">state</span>: MI
    <span style="color: #0000ff;">postal</span>: <span style="color: #ff6600;">48046</span></pre>
<p>Here&#8217;s how to write the above example with aliases:</p>
<pre>
<pre><span style="color: #0000ff;">bill-to</span>: <span style="color: #993300;">&amp;id001</span>
  <span style="color: #0000ff;">given</span>: Chris
  <span style="color: #0000ff;">family</span>: Dumars
  <span style="color: #0000ff;">address</span>:
    <span style="color: #0000ff;">lines</span>: |
<span style="color: #999999;">      458 Walman Dr.
      Suite #292</span>
    <span style="color: #0000ff;">city</span>: Royal Oak
    <span style="color: #0000ff;">state</span>: MI
    <span style="color: #0000ff;">postal</span>: <span style="color: #ff6600;">48046</span>
<span style="color: #0000ff;">ship-to</span>: <span style="color: #993300;">*id001
</span></pre>
</pre>
<p>And even better&#8230; you can override previous values, or even add extras by &#8216;merging&#8217; an anchor:</p>
<pre>
<pre><span style="color: #0000ff;">bill-to</span>: <span style="color: #993300;">&amp;id001</span>
  <span style="color: #0000ff;">given</span>: Chris
  <span style="color: #0000ff;">family</span>: Dumars
  <span style="color: #0000ff;">address</span>:
    <span style="color: #0000ff;">lines</span>: |
<span style="color: #999999;">      458 Walman Dr.
      Suite #292</span>
    <span style="color: #0000ff;">city</span>: Royal Oak
    <span style="color: #0000ff;">state</span>: MI
    <span style="color: #0000ff;">postal</span>: <span style="color: #ff6600;">48046</span>
<span style="color: #0000ff;">ship-to</span>:
  <span style="color: #0000ff;">&lt;&lt;</span>: <span style="color: #800000;">*id001</span>
  <span style="color: #0000ff;">given</span>: Sam</pre>
</pre>
<p>This would have been a fantastic alternative to what we have already created, however, YAML to PHP parsers don&#8217;t seem to like the merging syntax <img src='http://www.inspirelabs.co.uk/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.inspirelabs.co.uk/14/08/2009/yaml-reference-card/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing the PHP Session Lifetime</title>
		<link>http://www.inspirelabs.co.uk/04/05/2009/changing-the-php-session-lifetime/</link>
		<comments>http://www.inspirelabs.co.uk/04/05/2009/changing-the-php-session-lifetime/#comments</comments>
		<pubDate>Mon, 04 May 2009 19:32:52 +0000</pubDate>
		<dc:creator>Adam Pancutt</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://inspirelabs.co.uk/?p=40</guid>
		<description><![CDATA[As anyone who has attempted to use the default PHP session handler has probably noticed, sessions naturally die after a period of inactivity. What you&#8217;ll probably want in most situations is for the session not to die (or &#8216;expire&#8217;) until the user closes their browser. Unfortunately, there is no server-side way of knowing for certain [...]]]></description>
			<content:encoded><![CDATA[<p>As anyone who has attempted to use the default PHP session handler has probably noticed, sessions naturally die after a period of inactivity.</p>
<p>What you&#8217;ll probably want in most situations is for the session not to die (or &#8216;expire&#8217;) until the user closes their browser. Unfortunately, there is no server-side way of knowing <strong>for certain</strong> when the browser is closed and can only assume it has been closed after a period of inactivity (user hasn&#8217;t made any requests).</p>
<p>First, let&#8217;s take a brief look at how the default PHP session handler works&#8230;</p>
<p>Before a session can be used, the <a href="http://php.net/session_start" target="_blank"><code>session_start()</code></a> function must be called. This function will perform the necessary steps to initialise a new session and read in any data from an existing one. In addition, this method also does some tidying up which ultimately kills your sessions prematurely.</p>
<p>To keep session data tidy on your server, PHP will perform some &#8216;garbage collection&#8217; (GC) &#8212; that is, PHP will search for any session data that it thinks is old then deletes it. The default session handler uses a <code>session.gc_maxlifetime</code> configuration option which allows the application to set the lifetime of its session. This value is in seconds and defaults to 1440 (24 minutes). Therefore, any sessions that have been inactive for more than 24 minutes will be considered dead. This is not very helpful in Content Management Systems where it can take longer for the user to write their copy, only to be logged out when they come to submit the content.</p>
<p>One other little quirk that PHP likes to throw at you is the issue that arises when attempting to store session data with varying lifetimes in the same directory. What the default handler will do in this case is get the shortest lifetime and apply it to all sessions in the same folder. For example, let&#8217;s say you have Project A which used a session lifetime of 1440 and Project B which used 2400, both storing their session data in <code>/tmp/sessions</code>. It&#8221;ll save you a headache to know that Project B sessions will die after 1440 seconds. There are two obvious solutions for this: 1) create a subfolder for each project (e.g. <code>/tmp/sessions/myproject</code>; or 2) create a subfolder for each lifetime (e.g. <code>/tmp/sessions/1440</code>).</p>
<p>As a sidenote &#8212; to minimise the load that GC process puts on the server, PHP will not perform a GC every time the <code>session_start()</code> is called. Instead, the directives <code>session.gc_probability</code> and <code>session.gc_divisor</code> can be set. For example, 1 and 100, respectively, means that there is a 1-in-100 (1%) chance that PHP will perform a GC when a session is started (more information available in the <a href="http://php.net/manual/en/session.configuration.php" target="_blank">PHP manual</a>).</p>
<p>So, how do we change the default lifetime of 24 minutes? There are two solutions:</p>
<ol>
<li>Play around with a few PHP settings, either in the php.ini file or at runtime using <a href="http://php.net/ini_set" target="_blank"><code>ini_set()</code></a>, so that they have a longer lifetime</li>
<li>Create a custom session handler with a more complex GC strategy</li>
</ol>
<p>In most instances you&#8217;ll probably go for the first option as it is relatively simple and probably faster than most custom solutions. So, to get you started, here is a snippet of code which will create a 24-hour session (you could class this as an &#8216;infinite&#8217; session for most typical cases).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Choose your desired lifetime</span>
<span style="color: #000088;">$lifetime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">86400</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 24 hours</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Consider sessions dead after 24 hours</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'session.gc_maxlifetime'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lifetime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Store the sessions in a subdirectory based on the lifetime</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'session.save_path'</span><span style="color: #339933;">,</span> <span style="color: #990000;">ini_get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'session.save_path'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$lifetime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Now start the session as usual</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let us know how you get on!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.inspirelabs.co.uk/04/05/2009/changing-the-php-session-lifetime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

