<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: oop code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 08 Oct 2008 03:42:08 GMT</pubDate>
    <description>DZone Snippets: oop code</description>
    <item>
      <title>Javascript Ajax Object</title>
      <link>http://snippets.dzone.com/posts/show/3650</link>
      <description>&lt;code&gt;&lt;br /&gt;// Just a stub function we'll tell ajaxObject to call when it's done&lt;br /&gt;// callback functions get responseText, and responseStat respectively&lt;br /&gt;// in their arguments.&lt;br /&gt;function fin(responseTxt,responseStat) {&lt;br /&gt;  alert(responseStat+' - '+responseTxt);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// create a new ajaxObject, give it a url it will be calling and&lt;br /&gt;// tell it to call the function "fin" when its got data back from the server.&lt;br /&gt;var test1 = new ajaxObject('http://someurl.com/server.cgi',fin);&lt;br /&gt;    test1.update();&lt;br /&gt;		&lt;br /&gt;// create a new ajaxObject, give it a url and tell it to call fin when it&lt;br /&gt;// gets data back from the server.  When we initiate the ajax call we'll&lt;br /&gt;// be passing 'id=user4379' to the server.		&lt;br /&gt;var test2 = new ajaxObject('http://someurl.com/program.php',fin);&lt;br /&gt;    test2.update('id=user4379');&lt;br /&gt;		&lt;br /&gt;// create a new ajaxObject but we'll overwrite the callback function inside&lt;br /&gt;// the object to more tightly bind the object with the response hanlder.&lt;br /&gt;var test3 = new ajaxObject('http://someurl.com/prog.py', fin);&lt;br /&gt;    test3.callback = function (responseTxt, responseStat) {&lt;br /&gt;      // we'll do something to process the data here.&lt;br /&gt;      document.getElementById('someDiv').innerHTML=responseTxt;&lt;br /&gt;    }&lt;br /&gt;    test3.update('coolData=47&amp;userId=user49&amp;log=true');	&lt;br /&gt;		&lt;br /&gt;// create a new ajaxObject and pass the data to the server (in update) as&lt;br /&gt;// a POST method instead of a GET method.&lt;br /&gt;var test4 = new ajaxObject('http://someurl.com/postit.cgi', fin);&lt;br /&gt;    test4.update('coolData=47&amp;userId=user49&amp;log=true','POST');	&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function ajaxObject(url, callbackFunction) {&lt;br /&gt;  var that=this;      &lt;br /&gt;  this.updating = false;&lt;br /&gt;  this.abort = function() {&lt;br /&gt;    if (that.updating) {&lt;br /&gt;      that.updating=false;&lt;br /&gt;      that.AJAX.abort();&lt;br /&gt;      that.AJAX=null;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  this.update = function(passData,postMethod) { &lt;br /&gt;    if (that.updating) { return false; }&lt;br /&gt;    that.AJAX = null;                          &lt;br /&gt;    if (window.XMLHttpRequest) {              &lt;br /&gt;      that.AJAX=new XMLHttpRequest();              &lt;br /&gt;    } else {                                  &lt;br /&gt;      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");&lt;br /&gt;    }                                             &lt;br /&gt;    if (that.AJAX==null) {                             &lt;br /&gt;      return false;                               &lt;br /&gt;    } else {&lt;br /&gt;      that.AJAX.onreadystatechange = function() {  &lt;br /&gt;        if (that.AJAX.readyState==4) {             &lt;br /&gt;          that.updating=false;                &lt;br /&gt;          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        &lt;br /&gt;          that.AJAX=null;                                         &lt;br /&gt;        }                                                      &lt;br /&gt;      }                                                        &lt;br /&gt;      that.updating = new Date();                              &lt;br /&gt;      if (/post/i.test(postMethod)) {&lt;br /&gt;        var uri=urlCall+'?'+that.updating.getTime();&lt;br /&gt;        that.AJAX.open("POST", uri, true);&lt;br /&gt;        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");&lt;br /&gt;        that.AJAX.send(passData);&lt;br /&gt;      } else {&lt;br /&gt;        var uri=urlCall+'?'+passData+'&amp;timestamp='+(that.updating.getTime()); &lt;br /&gt;        that.AJAX.open("GET", uri, true);                             &lt;br /&gt;        that.AJAX.send(null);                                         &lt;br /&gt;      }              &lt;br /&gt;      return true;                                             &lt;br /&gt;    }                                                                           &lt;br /&gt;  }&lt;br /&gt;  var urlCall = url;        &lt;br /&gt;  this.callback = callbackFunction || function () { };&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Mar 2007 03:06:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3650</guid>
      <author>pcx99 (Patrick)</author>
    </item>
    <item>
      <title>BlankSlate: Clean an object class to its bare minimum methods in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/1873</link>
      <description>From &lt;a href="http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc"&gt;http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;class BlankSlate&lt;br /&gt;   instance_methods.each { |m| undef_method m unless m =~ /^__/ }&lt;br /&gt;end&lt;/code&gt;</description>
      <pubDate>Sun, 09 Apr 2006 23:46:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1873</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Lua: Descending Table Traversal Round 2: Iterator Object</title>
      <link>http://snippets.dzone.com/posts/show/940</link>
      <description>This is some concept code I threw together for Lua to see how feasable it was to make a lua object/table into an iterator that you can manipulate.  This iterator descends through all tables and subtables of any queued table.  It returns the "path" we're at, as a string, the current table, the key, and the value.&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;code&gt;traverse = {}&lt;br /&gt;function traverse:new(tname)&lt;br /&gt;	local o = {}&lt;br /&gt;	o.names = {}&lt;br /&gt;	o.queue = {}&lt;br /&gt;	o.cur = {&lt;br /&gt;		tbl = nil,&lt;br /&gt;		path = nil,&lt;br /&gt;		state = nil,&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	local mt = {}&lt;br /&gt;	function mt:__call(tn)&lt;br /&gt;		return o:iter(tn)&lt;br /&gt;	end&lt;br /&gt;	mt.__index = self&lt;br /&gt;	setmetatable(o, mt)&lt;br /&gt;&lt;br /&gt;	if tname then&lt;br /&gt;		o:enqueue(tname)&lt;br /&gt;	end&lt;br /&gt;	return o&lt;br /&gt;end&lt;br /&gt;function traverse:next()&lt;br /&gt;	local v&lt;br /&gt;	local names, queue, cur = self.names, self.queue, self.cur&lt;br /&gt;&lt;br /&gt;	local function _poptbl()&lt;br /&gt;		if cur.tbl then&lt;br /&gt;			names[cur.tbl] = nil&lt;br /&gt;		end&lt;br /&gt;		cur.tbl = table.remove(queue, 1)&lt;br /&gt;		cur.path = names[cur.tbl]&lt;br /&gt;		cur.state = nil&lt;br /&gt;	end&lt;br /&gt;&lt;br /&gt;	repeat&lt;br /&gt;		-- Find something to return to the user...&lt;br /&gt;		if not cur.state then&lt;br /&gt;			-- Pop a new table off the stack&lt;br /&gt;			_poptbl()&lt;br /&gt;			if not cur.tbl then&lt;br /&gt;				-- No more tables to process&lt;br /&gt;				return nil&lt;br /&gt;			end&lt;br /&gt;		end&lt;br /&gt;		cur.state,v = next(cur.tbl, cur.state)&lt;br /&gt;	until cur.state&lt;br /&gt;&lt;br /&gt;	if type(v) == "table" then&lt;br /&gt;		local path = cur.path.."."..cur.state&lt;br /&gt;		names[v] = path&lt;br /&gt;		table.insert(queue, v)&lt;br /&gt;	end&lt;br /&gt;	return cur.path,cur.tbl,cur.state,v&lt;br /&gt;end&lt;br /&gt;function traverse:iter(tname)&lt;br /&gt;	if tname then&lt;br /&gt;		self:enqueue(tname)&lt;br /&gt;	end&lt;br /&gt;	return function(...) return self:next(unpack(arg)) end, nil, nil&lt;br /&gt;end&lt;br /&gt;function traverse:enqueue(tname)&lt;br /&gt;	local v = _G[tname]&lt;br /&gt;	table.insert(self.queue, v)&lt;br /&gt;	self.names[v] = tname&lt;br /&gt;end&lt;br /&gt;function traverse:reset()&lt;br /&gt;	self.names = {}&lt;br /&gt;	self.queue = {}&lt;br /&gt;end&lt;br /&gt;local traverse_mt = {&lt;br /&gt;    __call = function(self, tname)&lt;br /&gt;             	if tname then&lt;br /&gt;             		return self:new(tname):iter()&lt;br /&gt;             	else&lt;br /&gt;             		return self:new()&lt;br /&gt;             	end&lt;br /&gt;             end&lt;br /&gt;}&lt;br /&gt;setmetatable(traverse, traverse_mt)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example Usage:&lt;br /&gt;&lt;code&gt;foo = {a={},b={},c={"bar"},d={e={},f={"moo"}},1,2,3,4,5}&lt;br /&gt;bar = {"alpha", "beta", "theta", omega = {}}&lt;br /&gt;&lt;br /&gt;local mytraverser = traverse()&lt;br /&gt;mytraverser:enqueue("bar")&lt;br /&gt;mytraverser:enqueue("foo")&lt;br /&gt;for p,t,k,v in mytraverser() do&lt;br /&gt;--for p,t,k,v in traverse('foo') do&lt;br /&gt;	print(string.format("%s[%s] = %s",tostring(p),tostring(k),tostring(v)))&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Output from Example Usage:&lt;br /&gt;&lt;code&gt;bar[1] = alpha&lt;br /&gt;bar[2] = beta&lt;br /&gt;bar[3] = theta&lt;br /&gt;bar[omega] = table: 0x510650&lt;br /&gt;foo[1] = 1&lt;br /&gt;foo[2] = 2&lt;br /&gt;foo[3] = 3&lt;br /&gt;foo[4] = 4&lt;br /&gt;foo[5] = 5&lt;br /&gt;foo[a] = table: 0x5102d0&lt;br /&gt;foo[c] = table: 0x510370&lt;br /&gt;foo[b] = table: 0x510320&lt;br /&gt;foo[d] = table: 0x5103c0&lt;br /&gt;foo.c[1] = bar&lt;br /&gt;foo.d[e] = table: 0x5104c0&lt;br /&gt;foo.d[f] = table: 0x510510&lt;br /&gt;foo.d.f[1] = moo&lt;/code&gt;</description>
      <pubDate>Mon, 05 Dec 2005 13:27:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/940</guid>
      <author>kergoth (Chris Larson)</author>
    </item>
    <item>
      <title>PHP5 __sleep to handle superclasses' private members</title>
      <link>http://snippets.dzone.com/posts/show/374</link>
      <description>PHP5 introduces visibility for object/class methods and data members.  This can prove problematic when serializing objects because base classes cannot see their parents' private vars, so these can't be serialized.  Plus, a base class probably doesn't know about all of its parents' private vars (even its public and protected ones).  So, I use this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class Object {&lt;br /&gt;    function __sleep() {&lt;br /&gt;        return array_keys( (array)$this );&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class BaseClass extends Object {&lt;br /&gt;    private $foo;&lt;br /&gt;    private $dbh; // some resource, like a database handler&lt;br /&gt;    function __sleep() {&lt;br /&gt;        unset($this-&gt;dbh);&lt;br /&gt;        return parent::__sleep();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class SubClass extends BaseClass {&lt;br /&gt;    private $bar;&lt;br /&gt;    function __sleep() {&lt;br /&gt;        return parent::__sleep();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The point is, the subclasses do any local cleanup then delegate the actual __sleep request to a parent class.  With a common parent class Object that all other classes extend, we can keep the real serialize code in one place but still allow children to change their state before sleeping.</description>
      <pubDate>Thu, 09 Jun 2005 00:44:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/374</guid>
      <author>tbarstow (Taylor)</author>
    </item>
    <item>
      <title>Flexible Singleton</title>
      <link>http://snippets.dzone.com/posts/show/135</link>
      <description>PHP function Singleton allows you holding exactly one object of a class in memory. If an object of a given class doesn't exist, Singleton will create and store a new one in static array singleton. It then (and otherwise) will return a reference to it.&lt;br /&gt;&lt;br /&gt;Adding an optional ID allows holding more than one object a class. Assigned ID will distinguish array keys.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt; * Singleton Repository&lt;br /&gt; * @param string $class PHP Class Name&lt;br /&gt; * @param string $id Optional Object ID&lt;br /&gt; * @return reference Reference to existing Object&lt;br /&gt; */&lt;br /&gt;function &amp;Singleton($class, $id='') {&lt;br /&gt;  static $singleton = array();&lt;br /&gt;  if (!array_key_exists($class.$id, $singleton))&lt;br /&gt;    $singleton[$class.$id] = &amp;new $class();&lt;br /&gt;  $reference = &amp;$singleton[$class.$id];&lt;br /&gt;  return $reference;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Use like this:&lt;br /&gt;[edit] I'm sorry there was a mistake in the first exmaple for three days or so. Fixed.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# first call: create object&lt;br /&gt;$site_user=&amp;Singleton('Student');&lt;br /&gt;$site_user-&gt;Drink_Beer(5);&lt;br /&gt;&lt;br /&gt;# second call: get a reference&lt;br /&gt;$current_user=&amp;Singleton('Student');&lt;br /&gt;echo $current_user-&gt;Show_Beers_Counter();&lt;br /&gt;#will be 5&lt;br /&gt;&lt;br /&gt;#Two different objects&lt;br /&gt;$one=&amp;Singleton('Some_Class','one');&lt;br /&gt;$two=&amp;Singleton('Some_Class','two');&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Works fine with PHP4, not tested on PHP5</description>
      <pubDate>Mon, 11 Apr 2005 05:52:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/135</guid>
      <author>drwitt (Carsten Witt)</author>
    </item>
    <item>
      <title>generic object</title>
      <link>http://snippets.dzone.com/posts/show/80</link>
      <description>a generic object type thing for perl where you can use setanything and getanything to set and get properties via the AUTOLOAD method.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package thing;&lt;br /&gt;&lt;br /&gt;use strict;&lt;br /&gt;&lt;br /&gt;sub new {&lt;br /&gt;	my %self;&lt;br /&gt;	my ($class,@rest) = @_;&lt;br /&gt;	unless ($#rest%2) { die "Odd parameter count\n"}&lt;br /&gt;	for (my $k=0; $k&lt;@rest; $k+=2) {&lt;br /&gt;		my $wotsit = lc($rest[$k]);&lt;br /&gt;		$wotsit =~ s/-//;&lt;br /&gt;		$self{$wotsit} = $rest[$k+1];&lt;br /&gt;		}&lt;br /&gt;	bless \%self,$class;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub AUTOLOAD {&lt;br /&gt;        my ($where,$val) = @_;&lt;br /&gt;        our $AUTOLOAD;&lt;br /&gt;        $AUTOLOAD =~ s/.*://;&lt;br /&gt;        my ($dirn,$what) = ($AUTOLOAD =~ /(...)(.*)/);&lt;br /&gt;        if ($dirn eq "get") {&lt;br /&gt;                return $$where{lc($what)};&lt;br /&gt;        } elsif ($dirn eq "set") {&lt;br /&gt;                $$where{lc($what)} = $val;&lt;br /&gt;        }&lt;br /&gt;        return 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;1;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here's a quick example of usage&lt;br /&gt;&lt;code&gt;&lt;br /&gt;use thing&lt;br /&gt;&lt;br /&gt;$house = new thing("type","building","rooms",12,"foundations", "yes");&lt;br /&gt;&lt;br /&gt;print $house -&gt; gettype();&lt;br /&gt;$house -&gt; settype("tree");&lt;br /&gt;print " " . $house -&gt; gettype();&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Apr 2005 18:00:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/80</guid>
      <author>tomp (tomp)</author>
    </item>
    <item>
      <title>custom accessors</title>
      <link>http://snippets.dzone.com/posts/show/8</link>
      <description>&lt;code&gt;class Song &lt; ActiveRecord::Base&lt;br /&gt;  # Uses an integer of seconds to hold the length of the song&lt;br /&gt;&lt;br /&gt;  def length=(minutes)&lt;br /&gt;    write_attribute("length", minutes * 60)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def length&lt;br /&gt;    read_attribute("length") / 60&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;</description>
      <pubDate>Sat, 02 Apr 2005 19:39:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/8</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
