<?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: Mixins in PHP</title>
	<atom:link href="http://www.jansch.nl/2006/08/23/mixins-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jansch.nl/2006/08/23/mixins-in-php/</link>
	<description>Ivo's blog about PHP, the internet and life in general</description>
	<lastBuildDate>Wed, 08 Sep 2010 18:11:07 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Schleicher</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8671</link>
		<dc:creator>Schleicher</dc:creator>
		<pubDate>Tue, 15 May 2007 13:01:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8671</guid>
		<description>I have similar to previous comment Mixin realisation.
Tested ans deployed in some projects, works well.

described in my blog:

http://www.schleicher.ru/blog/183.html 

Sorry, most text in russian, but code is plain...</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->I have similar to previous comment Mixin realisation.<br />
Tested ans deployed in some projects, works well.</p>
<p>described in my blog:</p>
<p><a href="http://www.schleicher.ru/blog/183.html" rel="nofollow">http://www.schleicher.ru/blog/183.html</a> </p>
<p>Sorry, most text in russian, but code is plain&#8230;<!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jasper Bekkers</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8601</link>
		<dc:creator>Jasper Bekkers</dc:creator>
		<pubDate>Tue, 01 May 2007 16:42:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8601</guid>
		<description>Too bad we cannot reassign the $this variable anymore, it would have been extremely useful in this particular case. Thankfully we can work around it :).

My solution, that doesn&#039;t use eval, is supplied below :).
[code]
class Mixin
{
    private $me;
    
    public function setMe($newThis)
    {
         $this-&gt;me = $newThis;
    }
    
    public function __get($key)
    {
        return $this-&gt;me-&gt;$key;
    }
    
    public function __call($method, $arguments)
    {
        return call_user_func_array(
            array($this-&gt;me, $method), 
            $arguments
        );
    }
    
    public function __set($key, $value)
    {
        $this-&gt;me-&gt;$key = $value;
    }
}

class Object
{
    private $mixinLookup_ = array();
    
    public function __construct()
    {
        if(is_array($this-&gt;mixins))
        {
            foreach($this-&gt;mixins as $mixin)
            {
                $methods = get_class_methods($mixin);
                foreach((array)$methods as $method)
                    $this-&gt;mixinLookup_[$method] = new $mixin;
            }
        }
    }
    
    public function __call($method, $arguments)
    {
        if(isset($this-&gt;mixinLookup_[$method]))
        {
            $this-&gt;mixinLookup_[$method]-&gt;setMe($this);
            return call_user_func_array(
                array($this-&gt;mixinLookup_[$method], $method), 
                $arguments
            );
        }
    }
}

class Boldable extends Mixin
{
    public function bold()
    {
        return &quot;&lt;b&gt;&quot; . $this-&gt;toString() . &quot;&lt;/b&gt;&quot;;
    }
}

class Nixim extends Object
{
    protected $mixins = array(&quot;Boldable&quot;);
    
    public function toString()
    {
        return &quot;Nixim&quot;;
    }
}

$n = new Nixim();
echo $n-&gt;bold();
[/code]

PS. Although the post is probably considered old, it still inspired me to write this code (it&#039;s just a couple of minutes old) and share it with you guys :).</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->Too bad we cannot reassign the $this variable anymore, it would have been extremely useful in this particular case. Thankfully we can work around it <img src='http://www.jansch.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>My solution, that doesn&#8217;t use eval, is supplied below <img src='http://www.jansch.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />
[code]<br />
class Mixin<br />
{<br />
    private $me;</p>
<p>    public function setMe($newThis)<br />
    {<br />
         $this->me = $newThis;<br />
    }</p>
<p>    public function __get($key)<br />
    {<br />
        return $this->me->$key;<br />
    }</p>
<p>    public function __call($method, $arguments)<br />
    {<br />
        return call_user_func_array(<br />
            array($this->me, $method),<br />
            $arguments<br />
        );<br />
    }</p>
<p>    public function __set($key, $value)<br />
    {<br />
        $this->me->$key = $value;<br />
    }<br />
}</p>
<p>class Object<br />
{<br />
    private $mixinLookup_ = array();</p>
<p>    public function __construct()<br />
    {<br />
        if(is_array($this->mixins))<br />
        {<br />
            foreach($this->mixins as $mixin)<br />
            {<br />
                $methods = get_class_methods($mixin);<br />
                foreach((array)$methods as $method)<br />
                    $this->mixinLookup_[$method] = new $mixin;<br />
            }<br />
        }<br />
    }</p>
<p>    public function __call($method, $arguments)<br />
    {<br />
        if(isset($this->mixinLookup_[$method]))<br />
        {<br />
            $this->mixinLookup_[$method]->setMe($this);<br />
            return call_user_func_array(<br />
                array($this->mixinLookup_[$method], $method),<br />
                $arguments<br />
            );<br />
        }<br />
    }<br />
}</p>
<p>class Boldable extends Mixin<br />
{<br />
    public function bold()<br />
    {<br />
        return "<b>" . $this->toString() . "</b>";<br />
    }<br />
}</p>
<p>class Nixim extends Object<br />
{<br />
    protected $mixins = array("Boldable");</p>
<p>    public function toString()<br />
    {<br />
        return "Nixim";<br />
    }<br />
}</p>
<p>$n = new Nixim();<br />
echo $n->bold();<br />
[/code]</p>
<p>PS. Although the post is probably considered old, it still inspired me to write this code (it&#8217;s just a couple of minutes old) and share it with you guys <img src='http://www.jansch.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kaiser</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8371</link>
		<dc:creator>kaiser</dc:creator>
		<pubDate>Sat, 09 Dec 2006 05:58:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8371</guid>
		<description>heya. i think i have a reason for one of your quirks--why does the &#039;static&#039; keyword throw the error?

looking at how you call a superclass (i.e. parent::__construct(), or parent::private_method()), it&#039;s obvious we&#039;re not truly calling a static method: instead, we&#039;re calling an instance method of another class.

declaring &#039;static&#039;, however, is saying that this method should not be associated with an instance. in ruby, though, i think you could get away with that depending on the calling context?

PHP isn&#039;t built like ruby for sure, which isn&#039;t a bad thing. there&#039;s something to be said about code that isn&#039;t ambigious by convention. you wouldn&#039;t believe how often i flip been @object.column_attribute and @object[&#039;column_attribute&#039;] within a rails app.</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->heya. i think i have a reason for one of your quirks&#8211;why does the &#8216;static&#8217; keyword throw the error?</p>
<p>looking at how you call a superclass (i.e. parent::__construct(), or parent::private_method()), it&#8217;s obvious we&#8217;re not truly calling a static method: instead, we&#8217;re calling an instance method of another class.</p>
<p>declaring &#8216;static&#8217;, however, is saying that this method should not be associated with an instance. in ruby, though, i think you could get away with that depending on the calling context?</p>
<p>PHP isn&#8217;t built like ruby for sure, which isn&#8217;t a bad thing. there&#8217;s something to be said about code that isn&#8217;t ambigious by convention. you wouldn&#8217;t believe how often i flip been @object.column_attribute and @object['column_attribute'] within a rails app.<!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hoho</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8271</link>
		<dc:creator>hoho</dc:creator>
		<pubDate>Wed, 27 Sep 2006 16:31:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8271</guid>
		<description>This is what i did.

 class Blinkable
  {private $_me;

    function blink()
    {
      echo &quot;&lt;blink&gt;&quot;.$this-&gt;_me-&gt;toString().&quot;&lt;/blink&gt;&quot;;
    }
  }

ANd then...

  function __call($method, $args)
  {
      if (isset($this-&gt;_mixinlookup[$method]))
    {
     return call_user_func_array(array($this-&gt;_mixinlookup[$method])), $method), $args);
    }
    trigger_error(&#039;Call to undefined function &#039;.$method, E_USER_WARNING);
  }</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->This is what i did.</p>
<p> class Blinkable<br />
  {private $_me;</p>
<p>    function blink()<br />
    {<br />
      echo &#8220;<blink>&#8220;.$this->_me->toString().&#8221;</blink>&#8220;;<br />
    }<br />
  }</p>
<p>ANd then&#8230;</p>
<p>  function __call($method, $args)<br />
  {<br />
      if (isset($this->_mixinlookup[$method]))<br />
    {<br />
     return call_user_func_array(array($this->_mixinlookup[$method])), $method), $args);<br />
    }<br />
    trigger_error(&#8216;Call to undefined function &#8216;.$method, E_USER_WARNING);<br />
  }<!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: johno</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8261</link>
		<dc:creator>johno</dc:creator>
		<pubDate>Wed, 30 Aug 2006 11:22:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8261</guid>
		<description>Done something similar here:
http://www.sitepoint.com/forums/showthread.php?t=332491</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->Done something similar here:<br />
<a href="http://www.sitepoint.com/forums/showthread.php?t=332491" rel="nofollow">http://www.sitepoint.com/forums/showthread.php?t=332491</a><!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivo Jansch</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8251</link>
		<dc:creator>Ivo Jansch</dc:creator>
		<pubDate>Wed, 30 Aug 2006 09:24:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8251</guid>
		<description>This is explained in the last part of the article, I cannot use call_user_func_array, as this will not call the method in such a way that $this is still accessible in the mixed in classes.</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->This is explained in the last part of the article, I cannot use call_user_func_array, as this will not call the method in such a way that $this is still accessible in the mixed in classes.<!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kapitan40</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8241</link>
		<dc:creator>Kapitan40</dc:creator>
		<pubDate>Wed, 30 Aug 2006 08:43:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8241</guid>
		<description>To avoid the eval
  eval(&quot;\$result = &quot;.$this-&gt;_mixinlookup[$method].&quot;::&quot;
            .$method.&quot;(&quot;.implode(&#039;,&#039;,$elems).&quot;);&quot;);

you may use this:
$result = call_user_func_array (array ($this-&gt;_mixinlookup[$method], $method), $args);</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->To avoid the eval<br />
  eval(&#8220;\$result = &#8220;.$this->_mixinlookup[$method].&#8221;::&#8221;<br />
            .$method.&#8221;(&#8220;.implode(&#8216;,&#8217;,$elems).&#8221;);&#8221;);</p>
<p>you may use this:<br />
$result = call_user_func_array (array ($this->_mixinlookup[$method], $method), $args);<!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivo Jansch</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8231</link>
		<dc:creator>Ivo Jansch</dc:creator>
		<pubDate>Tue, 29 Aug 2006 07:24:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8231</guid>
		<description>You are right :-), I misinterpreted your suggestion. This is indeed better as now objects are handled correctly too. I&#039;ve updated the example, thanks!</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->You are right <img src='http://www.jansch.nl/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> , I misinterpreted your suggestion. This is indeed better as now objects are handled correctly too. I&#8217;ve updated the example, thanks!<!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel 'Boo2M0rs0'</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8221</link>
		<dc:creator>Daniel 'Boo2M0rs0'</dc:creator>
		<pubDate>Tue, 29 Aug 2006 00:08:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8221</guid>
		<description>No, 

Look my code again.
The final evaluated code looks like:
&lt;code&gt;$result = Alertable::alert($args[0],$args[1]); //In the case there is two arguments&lt;/code&gt;

So arrays, objects, ressources and other special types are not converted to string.</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->No, </p>
<p>Look my code again.<br />
The final evaluated code looks like:<br />
<code>$result = Alertable::alert($args[0],$args[1]); //In the case there is two arguments</code></p>
<p>So arrays, objects, ressources and other special types are not converted to string.<!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivo Jansch</title>
		<link>http://www.jansch.nl/2006/08/23/mixins-in-php/comment-page-1/#comment-8211</link>
		<dc:creator>Ivo Jansch</dc:creator>
		<pubDate>Mon, 28 Aug 2006 23:56:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=461#comment-8211</guid>
		<description>The reason I used var_export on the variables, is that now it will also work for arrays, which end up as a litteral &#039;Array&#039; if I don&#039;t export them.</description>
		<content:encoded><![CDATA[<p><!-- google_ad_section_start -->The reason I used var_export on the variables, is that now it will also work for arrays, which end up as a litteral &#8216;Array&#8217; if I don&#8217;t export them.<!-- google_ad_section_end --></p>
]]></content:encoded>
	</item>
</channel>
</rss>
