<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: A dark corner of PHP: class casting</title>
	<atom:link href="http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/</link>
	<description>It&#039;s all about me, mysql and Einstein.</description>
	<lastBuildDate>Thu, 09 Sep 2010 15:08:00 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: kentpachi</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-217335</link>
		<dc:creator>kentpachi</dc:creator>
		<pubDate>Sat, 27 Mar 2010 18:04:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-217335</guid>
		<description>ADaniels &gt;  That will only set the public properties, not the private ones.

However a better alternative is to implement __set_state() as described by elias.

I think it will set privates too. if they have a setter.

if not this a design problem not implementation

if i do

$user-&gt;name = &quot;john&quot;

if name is public its ok or if it has magic setter (__set) or hard setter (setName($val)) method

so this will set the properties no matter its visibility</description>
		<content:encoded><![CDATA[<p>ADaniels &gt;  That will only set the public properties, not the private ones.</p>
<p>However a better alternative is to implement __set_state() as described by elias.</p>
<p>I think it will set privates too. if they have a setter.</p>
<p>if not this a design problem not implementation</p>
<p>if i do</p>
<p>$user-&gt;name = &#8220;john&#8221;</p>
<p>if name is public its ok or if it has magic setter (__set) or hard setter (setName($val)) method</p>
<p>so this will set the properties no matter its visibility</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nacho</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-214397</link>
		<dc:creator>Nacho</dc:creator>
		<pubDate>Wed, 10 Feb 2010 14:41:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-214397</guid>
		<description>Thanks for this!</description>
		<content:encoded><![CDATA[<p>Thanks for this!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peterchen</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-213281</link>
		<dc:creator>peterchen</dc:creator>
		<pubDate>Wed, 13 Jan 2010 15:08:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-213281</guid>
		<description>Hi Arnold, 

thanks for sharing this. I was struggling with copying my class manually and that&#039;s bullshit. Your version is nice although hacked and works like a charm :)

BR
Christoph</description>
		<content:encoded><![CDATA[<p>Hi Arnold, </p>
<p>thanks for sharing this. I was struggling with copying my class manually and that&#8217;s bullshit. Your version is nice although hacked and works like a charm <img src='http://www.jasny.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>BR<br />
Christoph</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aurelian Toma</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-211996</link>
		<dc:creator>Aurelian Toma</dc:creator>
		<pubDate>Fri, 18 Dec 2009 19:20:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-211996</guid>
		<description>i have the reverse :))

// lambda method declaration here
$methodHereIs = function() {
   ....
return &quot;someReturn&quot;;
}

$fullObjectOnTheFly = (object) array(
  &quot;aProperty&quot; =&gt; &quot;someValue&quot;,  #propertyOnTheFly
  &quot;aMethod&quot; =&gt; $methodHere.... #methodOnTheFly
   ....more</description>
		<content:encoded><![CDATA[<p>i have the reverse <img src='http://www.jasny.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</p>
<p>// lambda method declaration here<br />
$methodHereIs = function() {<br />
   &#8230;.<br />
return &#8220;someReturn&#8221;;<br />
}</p>
<p>$fullObjectOnTheFly = (object) array(<br />
  &#8220;aProperty&#8221; =&gt; &#8220;someValue&#8221;,  #propertyOnTheFly<br />
  &#8220;aMethod&#8221; =&gt; $methodHere&#8230;. #methodOnTheFly<br />
   &#8230;.more</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Morrell</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-211320</link>
		<dc:creator>Chris Morrell</dc:creator>
		<pubDate>Fri, 04 Dec 2009 14:55:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-211320</guid>
		<description>Another way to do this would be to use a modified version of the decorator pattern:

&lt;pre lang=&quot;php&quot;&gt;
class A
{
	protected $property = &#039;Set in A&#039;;
	
	public function getProperty()
	{
		return $this-&gt;property;
	}
	
	public function setProperty($property)
	{
		$this-&gt;property = $property;
	}
}

class B extends A
{
	public function __construct(A $class, $options = array())
	{
		$this-&gt;_class = $class;
	}
	
	public function __set($name, $value)
	{
		$this-&gt;_class-&gt;$name = $value;
	}
	
	public function __get($name)
	{
		return $this-&gt;_class-&gt;$name;
	}
	
	public function __isset($name)
	{
		return isset($this-&gt;_class-&gt;$name);
	}
	
	public function __unset($name)
	{
		unset($this-&gt;_class-&gt;$name);
	}
}

$a = new A();
$b = new B($a);

$b-&gt;setProperty(&#039;Set via B&#039;);
echo $b-&gt;getProperty();
&lt;/pre&gt;

Chris</description>
		<content:encoded><![CDATA[<p>Another way to do this would be to use a modified version of the decorator pattern:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">class</span> A
<span style="color: #66cc66;">&#123;</span>
	protected <span style="color: #0000ff;">$property</span> = <span style="color: #ff0000;">'Set in A'</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getProperty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&amp;gt;property;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setProperty<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$property</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&amp;gt;property = <span style="color: #0000ff;">$property</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> B <span style="color: #000000; font-weight: bold;">extends</span> A
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span>A <span style="color: #0000ff;">$class</span>, <span style="color: #0000ff;">$options</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&amp;gt;_class = <span style="color: #0000ff;">$class</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span>, <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$this</span>-&amp;gt;_class-&amp;gt;<span style="color: #0000ff;">$name</span> = <span style="color: #0000ff;">$value</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&amp;gt;_class-&amp;gt;<span style="color: #0000ff;">$name</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __isset<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&amp;gt;_class-&amp;gt;<span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __unset<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066;">unset</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&amp;gt;_class-&amp;gt;<span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$a</span> = <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$b</span> = <span style="color: #000000; font-weight: bold;">new</span> B<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$b</span>-&amp;gt;setProperty<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Set via B'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$b</span>-&amp;gt;getProperty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Chris</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arnold Daniels</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-210993</link>
		<dc:creator>Arnold Daniels</dc:creator>
		<pubDate>Fri, 27 Nov 2009 15:41:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-210993</guid>
		<description>&lt;strong&gt;&lt;em&gt;kentpachi:&lt;/em&gt;&lt;/strong&gt; That will only set the public properties, not the private ones.

However a better alternative is to implement &lt;a href=&quot;http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.set-state&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;__set_state()&lt;/a&gt; as described by elias.</description>
		<content:encoded><![CDATA[<p><strong><em>kentpachi:</em></strong> That will only set the public properties, not the private ones.</p>
<p>However a better alternative is to implement <a href="http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.set-state" target="_blank" rel="nofollow">__set_state()</a> as described by elias.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kentpachi</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-210987</link>
		<dc:creator>kentpachi</dc:creator>
		<pubDate>Fri, 27 Nov 2009 14:06:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-210987</guid>
		<description>why not this ?

&lt;pre lang=&quot;PHP&quot;&gt;
function ArrayToObject($array,$class)
{
 	$obj = new $class;
 	foreach($array as $k=&gt;$v)
 	{
 		$obj-&gt;$k = $v;
 	}
 	
 	return $obj;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>why not this ?</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">function</span> ArrayToObject<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span>,<span style="color: #0000ff;">$class</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
 	<span style="color: #0000ff;">$obj</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0000ff;">$class</span>;
 	<span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$k</span>=&amp;gt;<span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span>
 	<span style="color: #66cc66;">&#123;</span>
 		<span style="color: #0000ff;">$obj</span>-&amp;gt;<span style="color: #0000ff;">$k</span> = <span style="color: #0000ff;">$v</span>;
 	<span style="color: #66cc66;">&#125;</span>
&nbsp;
 	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$obj</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: zanlok</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-209401</link>
		<dc:creator>zanlok</dc:creator>
		<pubDate>Sat, 24 Oct 2009 06:59:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-209401</guid>
		<description>Unclean, and beautifully clever!</description>
		<content:encoded><![CDATA[<p>Unclean, and beautifully clever!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yury</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-208197</link>
		<dc:creator>Yury</dc:creator>
		<pubDate>Wed, 30 Sep 2009 05:34:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-208197</guid>
		<description>Recently I was attempting to cast stdClass resulted from json_decode to my own classes, that&#039;s what I&#039;ve came with:

http://freebsd.co.il/cast/

hope it&#039;ll be helpful to someone.</description>
		<content:encoded><![CDATA[<p>Recently I was attempting to cast stdClass resulted from json_decode to my own classes, that&#8217;s what I&#8217;ve came with:</p>
<p><a href="http://freebsd.co.il/cast/" rel="nofollow">http://freebsd.co.il/cast/</a></p>
<p>hope it&#8217;ll be helpful to someone.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samael S.</title>
		<link>http://www.jasny.net/articles/a-dark-corner-of-php-class-casting/comment-page-1/#comment-195643</link>
		<dc:creator>Samael S.</dc:creator>
		<pubDate>Sat, 30 May 2009 05:10:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/#comment-195643</guid>
		<description>@leo and Simon

Here&#039;s why it&#039;s usefull :

&lt;pre&gt;
class member
{
  public static function get ($id)
  {
    return casttoclass(&#039;member&#039;, 
      db_query (&quot;SELECT * FROM members WHERE id=$id LIMIT 1&quot;)
    );
  }

  public function getFullName ()
  {
    return $this-&gt;firstName.&#039; &#039;.$this-&gt;lastName;
  }
}
&lt;/pre&gt;

If you know some easiest way to do such a thing, tell me !</description>
		<content:encoded><![CDATA[<p>@leo and Simon</p>
<p>Here&#8217;s why it&#8217;s usefull :</p>
<pre>
class member
{
  public static function get ($id)
  {
    return casttoclass('member',
      db_query ("SELECT * FROM members WHERE id=$id LIMIT 1")
    );
  }

  public function getFullName ()
  {
    return $this-&gt;firstName.' '.$this-&gt;lastName;
  }
}
</pre>
<p>If you know some easiest way to do such a thing, tell me !</p>
]]></content:encoded>
	</item>
</channel>
</rss>
