<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3042167444859227071</id><updated>2011-04-21T19:42:37.206+02:00</updated><category term='mootools'/><category term='javascript'/><category term='php'/><category term='utility'/><category term='classes'/><category term='private'/><title type='text'>thomas.dullnig</title><subtitle type='html'>if( You.accept(Christ.sacrifice()) ){ You.life = new Life(); }</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://thomasdullnig.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3042167444859227071/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://thomasdullnig.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Thomas Dullnig</name><uri>http://www.blogger.com/profile/06299584623380746623</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='22' height='32' src='http://1.bp.blogspot.com/_fXyuZ3lVbZE/SPUWM5patOI/AAAAAAAAA8g/ZEnrNVhD5UI/S220/CIMG4060.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3042167444859227071.post-3050608721791887773</id><published>2009-01-27T09:32:00.006+01:00</published><updated>2009-01-27T10:13:08.579+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='mootools'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='utility'/><title type='text'>Mootools $A function in PHP (with a little extra 'force')</title><content type='html'>As you may have seen I'm a big of the &lt;a href="http://mootools.net/"&gt;mootools js library&lt;/a&gt;. I really love their $ functions as the are pretty handy in mostly every situation.&lt;br /&gt;&lt;br /&gt;But half of my programming time I'm working with the server-side mostly in PHP and sometimes there are situations where I would love to have this handy functions in my toolbox.&lt;br /&gt;&lt;br /&gt;So here comes one of them the &lt;a href="http://mootools.net/docs/Native/Array#A"&gt;$A functions&lt;/a&gt; which acts the quite the same as in mootools and has one thing extra, the 'force' option.&lt;br /&gt;&lt;pre name="code" class="PHP"&gt;&lt;br /&gt;function A($var, $force=false){&lt;br /&gt;  return ($force || !is_array($var) ? array($var) : $var);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;It checks if the variable is an array if yes than it returns the unchanged variable if not it wraps the variable in a new array. The force option bypass this test and wraps the variable in a new array even if it is already an array. That's all, simple but powerful.&lt;br /&gt;&lt;br /&gt;Usage:&lt;br /&gt;&lt;pre name="code" class="PHP"&gt;&lt;br /&gt;//Without forcing&lt;br /&gt;$var1 = 'hello world';&lt;br /&gt;$var1 = A($var1);&lt;br /&gt;//$var1 is wrapped =&gt; array('hello world')&lt;br /&gt;&lt;br /&gt;$fruits = array('apple', 'banana', 'cherry');&lt;br /&gt;$fruits = A($fruits);&lt;br /&gt;//$fruits is unchanged =&gt; array('apple', 'banana', 'cherry')&lt;br /&gt;&lt;br /&gt;//With forcing&lt;br /&gt;$fruits = A($fruits, true);&lt;br /&gt;//$fruits is wrapped =&gt; array(array('apple', 'banana', 'cherry'))&lt;br /&gt;&lt;/pre&gt;It helped me a lot and maybe it can help you too.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3042167444859227071-3050608721791887773?l=thomasdullnig.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://thomasdullnig.blogspot.com/feeds/3050608721791887773/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://thomasdullnig.blogspot.com/2009/01/mootools-function-in-php-with-little.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3042167444859227071/posts/default/3050608721791887773'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3042167444859227071/posts/default/3050608721791887773'/><link rel='alternate' type='text/html' href='http://thomasdullnig.blogspot.com/2009/01/mootools-function-in-php-with-little.html' title='Mootools $A function in PHP (with a little extra &apos;force&apos;)'/><author><name>Thomas Dullnig</name><uri>http://www.blogger.com/profile/06299584623380746623</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='22' height='32' src='http://1.bp.blogspot.com/_fXyuZ3lVbZE/SPUWM5patOI/AAAAAAAAA8g/ZEnrNVhD5UI/S220/CIMG4060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3042167444859227071.post-1194850005089379768</id><published>2009-01-22T15:36:00.006+01:00</published><updated>2009-01-23T00:17:37.649+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='classes'/><category scheme='http://www.blogger.com/atom/ns#' term='mootools'/><category scheme='http://www.blogger.com/atom/ns#' term='private'/><title type='text'>Private Members in Mootools (I do it my way...)</title><content type='html'>A topic that keeps me busy for a while now is how can I achieve private members in a Mootools classes without rewriting the whole Class.js?&lt;br /&gt;&lt;br /&gt;Today morning I stumpled upon &lt;a href="http://mcarthurgfx.com/blog/article/getting-private-variables-in-a-mootools-class"&gt;Sean McArthurs&lt;/a&gt; and &lt;a href="http://www.nwhite.net/2008/10/10/private-methods-with-mootools-update2/"&gt;Dean Whites&lt;/a&gt;  ideas to the topic, but I thought there must be a simpler way to achieve this.&lt;br /&gt;&lt;br /&gt;My idea was to have two different class-objects, one is public and doesn't know anything about the private members, the second object has the first object as its prototype extended with all private members. The second object gets bind to all methods of the class. So you can use all private members within each method of the class, while you can't use it outside of the class. Sounds complicated? Maybe, but I don't know how to explain it simpler. Ask me in German and I think I can give you a simpler explanation.&lt;br /&gt;&lt;br /&gt;Let's start with the class mutator:&lt;br /&gt;&lt;pre name="code" class="Jscript"&gt;&lt;br /&gt;Class.Mutators.Privates = function(self, privates){&lt;br /&gt;//From "Javascript - The Good Parts" (Douglas Crockford)&lt;br /&gt;var F = function () {};&lt;br /&gt;F.prototype = self;&lt;br /&gt;var privSelf = new F();&lt;br /&gt;&lt;br /&gt;$extend(privSelf, privates);&lt;br /&gt;&lt;br /&gt;for(var prop in self){&lt;br /&gt;if($type(self[prop]) === 'function' &amp;amp;&amp;amp; (prop !== 'parent' &amp;amp;&amp;amp; prop !== 'parentOf')){&lt;br /&gt; var parent = (self[prop]._parent_ || null);&lt;br /&gt; self[prop] = self[prop].bind(privSelf);&lt;br /&gt;&lt;br /&gt; if(parent !== null){&lt;br /&gt;  self[prop]._parent_ = parent;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;delete self['Privates'];&lt;br /&gt;for (var privProp in privates) {&lt;br /&gt;delete self['Privates'][privProp];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;privSelf.publicSelf = self;&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;That's all.&lt;br /&gt;I will explain it step by step:&lt;br /&gt;&lt;pre name="code" class="Jscript"&gt;&lt;br /&gt;var F = function () {};&lt;br /&gt;F.prototype = self;&lt;br /&gt;var privSelf = new F();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;As I explained above, making a new object as 'private object', that knows all private members, and has the 'public object' as prototype. So you can extend the class or 'public object' and your 'private object' can use the new members too, without extra work.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="Jscript"&gt;&lt;br /&gt;$extend(privSelf, privates);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now extend the 'private object' with the private members.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="Jscript"&gt;&lt;br /&gt;for(var prop in self){&lt;br /&gt;if($type(self[prop]) === 'function' &amp;amp;&amp;amp; (prop !== 'parent' &amp;amp;&amp;amp; prop !== 'parentOf')){&lt;br /&gt;var parent = (self[prop]._parent_ || null);&lt;br /&gt;self[prop] = self[prop].bind(privSelf);&lt;br /&gt;&lt;br /&gt;if(parent !== null){&lt;br /&gt; self[prop]._parent_ = parent;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Next loop through all properties of the 'public object' searching for methods we want to bind the 'private object' to. If we found one and it's not the mootools related methods 'parent' and 'parentOf' (they are needed for class extending), we cache its parent-function, bind the 'private object' to the public method and attach the parent function if the old function had one. We have to do this, or otherwise you can't use this.parent, when you extending a class.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="Jscript"&gt;&lt;br /&gt;delete self['Privates'];&lt;br /&gt;for (var privProp in privates) {&lt;br /&gt;delete self['Privates'][privProp];&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;So far so good, now do some cleanup.&lt;br /&gt;&lt;br /&gt;&lt;pre name="code" class="Jscript"&gt;&lt;br /&gt;privSelf.publicSelf = self;&lt;br /&gt;&lt;/pre&gt;This one is special. You need it to do chaining. As you can't return 'this' in your methods, remember you bound the 'private object' to all public methods, just returning 'this', will reveal all your private members. So I decide to attach the 'public object' to the 'private object'. If you want to return the class object for chaining, you can do something like 'return this.publicSelf'.&lt;br /&gt;&lt;br /&gt;My Test-Case:&lt;br /&gt;&lt;pre name="code" class="Jscript"&gt;window.addEvent('domready', function(){&lt;br /&gt;var Test0 = new Class({&lt;br /&gt;method0: function(){&lt;br /&gt; console.log('method0', this);&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;var Test1 = new Class({&lt;br /&gt;Privates: {&lt;br /&gt; 'secret': 'secret',&lt;br /&gt; 'secretMethod': function(){&lt;br /&gt;  console.log('secretMethod', this);&lt;br /&gt; }&lt;br /&gt;},&lt;br /&gt;&lt;br /&gt;Implements: [Options, Events],&lt;br /&gt;&lt;br /&gt;Extends: Test0,&lt;br /&gt;&lt;br /&gt;initialize: function(){&lt;br /&gt; this.secret = 'test1-secret';&lt;br /&gt; console.log('init', this);&lt;br /&gt;},&lt;br /&gt;&lt;br /&gt;publicMethod1: function(){&lt;br /&gt; console.log('publicMethod1', this);&lt;br /&gt; this.secretMethod();&lt;br /&gt; this.instanceMethod();&lt;br /&gt; this.implementedMethod();&lt;br /&gt;},&lt;br /&gt;&lt;br /&gt;setSecret: function(val){&lt;br /&gt; this.secret = val;&lt;br /&gt;},&lt;br /&gt;&lt;br /&gt;getSecret: function(){&lt;br /&gt; return this.secret;&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;var Test2 = new Class({&lt;br /&gt;Extends: Test1,&lt;br /&gt;&lt;br /&gt;Privates: {&lt;br /&gt; 'anotherSecret': 'secret2',&lt;br /&gt; 'secretObject': {&lt;br /&gt;  'prop1': 'save',&lt;br /&gt;  'prop2': 'and',&lt;br /&gt;  'prop3': 'sound'&lt;br /&gt; }&lt;br /&gt;},&lt;br /&gt;&lt;br /&gt;initialize: function(){&lt;br /&gt; this.anotherSecret = 'test2-secret';&lt;br /&gt;},&lt;br /&gt;&lt;br /&gt;publicMethod2: function(){&lt;br /&gt; console.log('publicMethod2', this);&lt;br /&gt; //Chaining without revealing your private members&lt;br /&gt; return this.publicSelf;&lt;br /&gt;},&lt;br /&gt;publicMethod1: function(){&lt;br /&gt; console.log('Test2.publicMethod1', this);&lt;br /&gt;},&lt;br /&gt;&lt;br /&gt;setSecret: function(val){&lt;br /&gt; this.anotherSecret = val;&lt;br /&gt;},&lt;br /&gt;&lt;br /&gt;getSecret: function(){&lt;br /&gt; return this.anotherSecret;&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;var t = new Test1();&lt;br /&gt;console.log('t', t);&lt;br /&gt;t.method0();&lt;br /&gt;&lt;br /&gt;t.instanceMethod = function(){&lt;br /&gt;console.log('instanceMethod', this);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Test1.implement({&lt;br /&gt;'implementedMethod': function(){&lt;br /&gt; console.log('implementedMethod', this);&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;t.implementedMethod();&lt;br /&gt;t.instanceMethod();&lt;br /&gt; &lt;br /&gt;var tt = new Test1();&lt;br /&gt;&lt;br /&gt;t.setSecret('t');&lt;br /&gt;tt.setSecret('tt');&lt;br /&gt;console.log('t', t.getSecret());&lt;br /&gt;console.log('tt', tt.getSecret(), 't', t.getSecret());&lt;br /&gt;&lt;br /&gt;var t2 = new Test2();&lt;br /&gt;console.log('t2', t2);&lt;br /&gt;var secretThis = t2.publicMethod2();&lt;br /&gt;console.log('secretThis', secretThis);&lt;br /&gt;t2.publicMethod1();&lt;br /&gt;t2.setSecret('t2 secret');&lt;br /&gt;console.log('t2', t2.getSecret());&lt;br /&gt;&lt;br /&gt;var t0 = new Test0();&lt;br /&gt;console.log('t0', t0);&lt;br /&gt;t0.method0();&lt;br /&gt;});&lt;br /&gt;&lt;/pre&gt;I know the test looks rather complicated and unstructered, oh and you need &lt;a href="http://getfirebug.com/"&gt;firebug&lt;/a&gt;, or &lt;a href="http://getfirebug.com/lite.html"&gt;firebug lite&lt;/a&gt; or something, as I use 'console.log' all over the place. But you can see very well when you get the 'public object' and when the 'private object'.&lt;br /&gt;&lt;br /&gt;Tell me what you think about it? Is it useful, or not? Are there any bugs in it?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3042167444859227071-1194850005089379768?l=thomasdullnig.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://thomasdullnig.blogspot.com/feeds/1194850005089379768/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://thomasdullnig.blogspot.com/2009/01/private-members-in-mootools-i-do-it-my.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3042167444859227071/posts/default/1194850005089379768'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3042167444859227071/posts/default/1194850005089379768'/><link rel='alternate' type='text/html' href='http://thomasdullnig.blogspot.com/2009/01/private-members-in-mootools-i-do-it-my.html' title='Private Members in Mootools (I do it my way...)'/><author><name>Thomas Dullnig</name><uri>http://www.blogger.com/profile/06299584623380746623</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='22' height='32' src='http://1.bp.blogspot.com/_fXyuZ3lVbZE/SPUWM5patOI/AAAAAAAAA8g/ZEnrNVhD5UI/S220/CIMG4060.JPG'/></author><thr:total>2</thr:total></entry></feed>
