<?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>webr3.org &#187; PHP</title>
	<atom:link href="http://webr3.org/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://webr3.org/blog</link>
	<description>brain&#039;s on fire!</description>
	<lastBuildDate>Tue, 19 Jul 2011 15:38:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>spl_object_hash is not unique</title>
		<link>http://webr3.org/blog/php/spl_object_hash-is-not-unique/</link>
		<comments>http://webr3.org/blog/php/spl_object_hash-is-not-unique/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 11:27:02 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[Cryptographic hash function]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[Hash function]]></category>
		<category><![CDATA[Hashing]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Search algorithms]]></category>
		<category><![CDATA[spl_object_hash]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=128</guid>
		<description><![CDATA[Here is why you should avoid using spl_object_hash() in PHP
from the documentation: "This function returns a unique identifier for the object. This id can be used as a hash key for storing objects or for identifying an object."
spl_object_hash() actually returns an md5 hash of the internal pointer of an object.
PHP efficiently reuses internal pointers; meaning [...]]]></description>
			<content:encoded><![CDATA[<p>Here is why you should avoid using spl_object_hash() in PHP</p>
<p>from the documentation: "This function returns a unique identifier for the object. This id can be used as a hash key for storing objects or for identifying an object."</p>
<p>spl_object_hash() actually returns an md5 hash of the internal pointer of an object.</p>
<p>PHP efficiently reuses internal pointers; meaning the second you remove an object the pointer is reused.</p>
<p>thus:</p>
<pre class="brush: php; title: ;">
&lt;?php
$transaction_ids = array();
for( $i=0; $i&lt;10; $i++ ) {
 $transaction = (object)('transaction ' . $i);
 $hash = spl_object_hash( $transaction );
 $transaction_ids[] = $hash;
}
print_r( $transaction_ids );
?&gt;
</pre>
<p>will output:</p>
<pre>Array
(
[0] =&gt; 4b8051a089b9e8352a97af234d4478a7
[1] =&gt; 076b44c4ffe009b98c0ba3348459c57a
[2] =&gt; 4b8051a089b9e8352a97af234d4478a7
[3] =&gt; 076b44c4ffe009b98c0ba3348459c57a
[4] =&gt; 4b8051a089b9e8352a97af234d4478a7
[5] =&gt; 076b44c4ffe009b98c0ba3348459c57a
[6] =&gt; 4b8051a089b9e8352a97af234d4478a7
[7] =&gt; 076b44c4ffe009b98c0ba3348459c57a
[8] =&gt; 4b8051a089b9e8352a97af234d4478a7
[9] =&gt; 076b44c4ffe009b98c0ba3348459c57a
)
</pre>
<p>not even remotely unique, could lead to serious problems, and simply.. I'd forget using it if I was you.</p>
<p><img class="alignnone size-full wp-image-130" title="sucks" src="http://webr3.org/blog/wp-content/uploads/2009/08/sucks.jpg" alt="sucks" width="600" height="250" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/php/spl_object_hash-is-not-unique/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Object to Array, Object to stdClass in PHP</title>
		<link>http://webr3.org/blog/php/object-to-array-object-to-stdclass-in-php/</link>
		<comments>http://webr3.org/blog/php/object-to-array-object-to-stdclass-in-php/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 12:53:12 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Data structures]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[OBJ]]></category>
		<category><![CDATA[Object]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Scripting languages]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=122</guid>
		<description><![CDATA[
ObjectConverter - one of those classes you won't see the point of until you hit the problem.
Problem: When you convert an object (instance of a class) to an array in PHP, only the public properties are included; often you need public, protected, private and inherited properties to be visible, indeed there isn't any real way [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-123" title="objectconverter" src="http://webr3.org/blog/wp-content/uploads/2009/08/objectconverter.jpg" alt="objectconverter" width="600" height="250" /></p>
<p><strong>ObjectConverter </strong>- <em>one of those classes you won't see the point of until you hit the problem</em>.</p>
<p><strong>Problem</strong>: When you convert an object (instance of a class) to an array in PHP, only the public properties are included; often you need public, protected, private and inherited properties to be visible, indeed there isn't any real way to expose all properties of an object without adding a "toArray" method to class itself; simply there is no simple way to do this (private properties are private for a reason!).</p>
<p><strong>Solution</strong>: ObjectConverter :) This is a utility class which will turn any given instance in to either an array or a bare stdClass instance; effectively exposing all properties so you can persist or transfer your objects easily.</p>
<p><strong>Note</strong>: afaik no other "object to array" script can do this, and after playing with multiple approaches over the years I finally settled on this implementation which does *not* use reflection or any such heavy weight trickery.</p>
<p><strong>Usage</strong>:<br />
$array = ObjectConverter::toArray( $obj );<br />
$stdClass = ObjectConverter::toStdClass( $obj );</p>
<p><strong>Download</strong>: <a href="http://webr3.org/experiments/php/ObjectConverter.zip">ObjectConverter PHP Script</a> (php5 only)</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/php/object-to-array-object-to-stdclass-in-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP Events - Actionscript 3 style events in PHP 5</title>
		<link>http://webr3.org/blog/experiments/php-events-actionscript-3-style-events-in-php-5/</link>
		<comments>http://webr3.org/blog/experiments/php-events-actionscript-3-style-events-in-php-5/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 17:33:12 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Event Listener]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[multi site systems]]></category>
		<category><![CDATA[Negation]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Serialization]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=111</guid>
		<description><![CDATA[Here are a set of classes which aim to provide basic actionscript style events in php5;
The Point - Events are useful everywhere even when you don't have a UI, the main reason for building this was to create a kind of "hook" system for php. Often we create multi site systems where each implementation of [...]]]></description>
			<content:encoded><![CDATA[<p>Here are a set of classes which aim to provide basic actionscript style events in php5;</p>
<p><strong>The Point </strong>- Events are useful everywhere even when you don't have a UI, the main reason for building this was to create a kind of "hook" system for php. Often we create multi site systems where each implementation of that system has different client specific considerations; consider a system where one client wants all of there images to go to amazon s3 instead of being saved on the host like your other clients. With this system you can simply:</p>
<ol>
<li>throw an "storeUpload" event which is cancelable</li>
<li>register a default event listener which listens for "storeUpload" events and provides default functionality, saving the upload and setting the location (ur) on the event.target</li>
<li>on one site register a custom event listener which listens for the "storeUpload" event, uploads to amazon s3, then cancels the event and set the event.target to the s3 url instead</li>
</ol>
<p>as you can see, with this you can keep the codebase of your multisite application the same and simply hook in to events whereever you want.</p>
<p>the system consists of 2 public classes:</p>
<h3>Event</h3>
<p>The base class for all events, provides event type and cancelable; with typical usage you create a new event, then dispatch it when ready.</p>
<ul>
<li><strong>__construct</strong>( $type , $cancelable=FALSE )<br />
create a new event listener - if you extend the Event class you must call this with parent::__construct( $type , $cancelable );</p>
<p><strong>$type</strong> - string, event type eg "debugEvent"<br />
<strong>$cancelable</strong> - boolean, provides cancelation functionality, so you can prevent further event listeners from firing thus in effect overriding them.</p>
<p>throws InvalidArgumentException if $type is not a string</li>
<li><strong>cancel</strong>() : void<br />
cancels an event, only if it is cancelable</li>
<li><strong>isCancelable</strong>() : Boolean</li>
<li><strong>isCanceled</strong>() : Boolean</li>
<li><strong>getType</strong>() : string</li>
<li><strong>getTarget</strong>() : obj<br />
target always contains the instance which dispatched the event.</li>
</ul>
<h3>EventDispatcher</h3>
<p>All classes which wish to dispatch events must inherit from EventDispatcher (like in most languages), this class provides:</p>
<ul>
<li><strong>addEventListener</strong>( $type , $callable , $owner ) : void<br />
adds an event listener, this may be a function, a class method or a static class method.<br />
note: a current limitation is that event listener methods must be public</p>
<p><strong>$type</strong> - string, event type eg "debugEvent"<br />
<strong>$callable</strong> - string, the function or method which is to be called when event $type is dispatched<br />
<strong>$owner</strong> - mixed, used when $callable is a method, can be either an object instance, or the string name of a class for static methods.</p>
<p><strong>throws:</strong><br />
InvalidArgumentException when type or callable isn't a string<br />
Exception('unknown method / function')</li>
<li><strong>removeEventListener</strong>( $type , $callable , $owner ) : void<br />
removes an event listener that has been added with addEventListener, this only removes a single event listener each call, so if you have 3 identical event listeners you will need to call this method 3 times to remove them all.</p>
<p>arguments are the same as addEventListener - throws no exceptions, always returns void</li>
<li><strong>hasEventListener</strong>( $type ) : Boolean<br />
checks if any event listeners are registered for event $type; always returns boolean true or false.</li>
<li><strong>dispatchEvent</strong>( Event $event ) : void<br />
the obvious one, dispatches an event and notifies all listeners.</li>
</ul>
<h3>Notes:</h3>
<p>The event dispatcher implements a function which hides all instance properties starting with __ from the serialize process, thus hiding the events and meaning your current serialization process will still work, additionally you can utilize it to hide any properties you don't want to be serialized.</p>
<p>I've tried to make the classes as simple, small and practical as possible, they execute rather quickly and shouldn't make any speed difference to your application - do however be aware that loosely coupling and entire system could allow coding errors to creep in, so maybe don't use it everywhere - but then PHP isn't strict anyways so..!</p>
<h2>Download</h2>
<p>You can download the <a href="http://webr3.org/experiments/php/PHPEvents.zip">PHP Event system</a> here (3 classes + example, 4.2k) - hope somebody finds it useful!</p>
<p><img class="alignnone size-full wp-image-112" title="events" src="http://webr3.org/blog/wp-content/uploads/2009/07/events.jpg" alt="events" width="600" height="250" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/experiments/php-events-actionscript-3-style-events-in-php-5/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

