<?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: Overloading constructors in PHP</title>
	<atom:link href="http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Fri, 23 Jul 2010 19:58:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Tiago Albineli Motta</title>
		<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/comment-page-1/#comment-1043</link>
		<dc:creator>Tiago Albineli Motta</dc:creator>
		<pubDate>Mon, 22 Sep 2008 16:58:14 +0000</pubDate>
		<guid isPermaLink="false">http://swedishfika.com/?p=71#comment-1043</guid>
		<description>It&#039;s cool, but i&#039;d rather to declare some error when some parameters combination doesn&#039;t fit:

function __construct($args)
{
$numArgs = sizeof($args);
switch ($numArgs)
{
case 1:
if(isset($args[&#039;a&#039;]))
{
$this-&gt;SetA($args[&#039;a&#039;]);
} else {
die(&quot;Parameter should be &#039;a&#039;&quot;);
}
break;
default: die(&quot;Should inform parameter &#039;a&#039;&quot;);
}
}</description>
		<content:encoded><![CDATA[<p>It&#8217;s cool, but i&#8217;d rather to declare some error when some parameters combination doesn&#8217;t fit:</p>
<p>function __construct($args)<br />
{<br />
$numArgs = sizeof($args);<br />
switch ($numArgs)<br />
{<br />
case 1:<br />
if(isset($args['a']))<br />
{<br />
$this-&gt;SetA($args['a']);<br />
} else {<br />
die(&#8220;Parameter should be &#8216;a&#8217;&#8221;);<br />
}<br />
break;<br />
default: die(&#8220;Should inform parameter &#8216;a&#8217;&#8221;);<br />
}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carl</title>
		<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/comment-page-1/#comment-712</link>
		<dc:creator>Carl</dc:creator>
		<pubDate>Sun, 24 Aug 2008 11:26:44 +0000</pubDate>
		<guid isPermaLink="false">http://swedishfika.com/?p=71#comment-712</guid>
		<description>Ahhah!

This is what I thinking about. i read this and could not figure out why I suddenly needed to write this bit of code.

http://www.hiveminds.co.uk/content/totally-useless-php-code-written-in-beer.html#comment-1018</description>
		<content:encoded><![CDATA[<p>Ahhah!</p>
<p>This is what I thinking about. i read this and could not figure out why I suddenly needed to write this bit of code.</p>
<p><a href="http://www.hiveminds.co.uk/content/totally-useless-php-code-written-in-beer.html#comment-1018" rel="nofollow">http://www.hiveminds.co.uk/content/totally-useless-php-code-written-in-beer.html#comment-1018</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piccolo Principe</title>
		<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/comment-page-1/#comment-711</link>
		<dc:creator>Piccolo Principe</dc:creator>
		<pubDate>Sun, 24 Aug 2008 10:38:42 +0000</pubDate>
		<guid isPermaLink="false">http://swedishfika.com/?p=71#comment-711</guid>
		<description>Maybe using static factory methods in the class could be the solution, like ::fromAB(), ::fromC() etc.
However the best approach could be define some setters in the class and to create a factory that take care of the instantiation of objects: this avoids mixing creation and business logic in the same class.</description>
		<content:encoded><![CDATA[<p>Maybe using static factory methods in the class could be the solution, like ::fromAB(), ::fromC() etc.<br />
However the best approach could be define some setters in the class and to create a factory that take care of the instantiation of objects: this avoids mixing creation and business logic in the same class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Reich</title>
		<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/comment-page-1/#comment-706</link>
		<dc:creator>Brian Reich</dc:creator>
		<pubDate>Sat, 23 Aug 2008 19:40:29 +0000</pubDate>
		<guid isPermaLink="false">http://swedishfika.com/?p=71#comment-706</guid>
		<description>What he said! 

I&#039;ve always used func_num_args() and func_get_args() to pull this off. You can use a switch or if/else to call another initialization method based on the number and type of arguments passed.

Having said that, what you mentioned reminds me a lot of the configuration array methodology I see in the Zend Framework.  Many class constructors in ZFW allow you to pass either a Zend_Config object or an associative array of options, rather than a long and confusing list of parameters.

You can make this method all the more powerful by creating a &quot;defaults&quot; array and using array_merge() to apply any defaults to the user&#039;s configuration that they didn&#039;t explicitly set.

I&#039;ve adopted this method in my own programming for initialization of any class that requires more than 2 or 3 initialization parameters.</description>
		<content:encoded><![CDATA[<p>What he said! </p>
<p>I&#8217;ve always used func_num_args() and func_get_args() to pull this off. You can use a switch or if/else to call another initialization method based on the number and type of arguments passed.</p>
<p>Having said that, what you mentioned reminds me a lot of the configuration array methodology I see in the Zend Framework.  Many class constructors in ZFW allow you to pass either a Zend_Config object or an associative array of options, rather than a long and confusing list of parameters.</p>
<p>You can make this method all the more powerful by creating a &#8220;defaults&#8221; array and using array_merge() to apply any defaults to the user&#8217;s configuration that they didn&#8217;t explicitly set.</p>
<p>I&#8217;ve adopted this method in my own programming for initialization of any class that requires more than 2 or 3 initialization parameters.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: k3n</title>
		<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/comment-page-1/#comment-679</link>
		<dc:creator>k3n</dc:creator>
		<pubDate>Fri, 22 Aug 2008 07:25:17 +0000</pubDate>
		<guid isPermaLink="false">http://swedishfika.com/?p=71#comment-679</guid>
		<description>That is one way to do it, althought I also like to use that technique if a method I&#039;m developing requires more than about 3 parameters. It provides lots of flexibility, without the problems that default values, ordering, and readability give you. Cheap named parameters.

You could also use func_get_args(), although I&#039;m not sure how references would be handled, and the code would be less ledgible.</description>
		<content:encoded><![CDATA[<p>That is one way to do it, althought I also like to use that technique if a method I&#8217;m developing requires more than about 3 parameters. It provides lots of flexibility, without the problems that default values, ordering, and readability give you. Cheap named parameters.</p>
<p>You could also use func_get_args(), although I&#8217;m not sure how references would be handled, and the code would be less ledgible.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Kjellberg</title>
		<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/comment-page-1/#comment-460</link>
		<dc:creator>Simon Kjellberg</dc:creator>
		<pubDate>Tue, 22 Jul 2008 16:41:44 +0000</pubDate>
		<guid isPermaLink="false">http://swedishfika.com/?p=71#comment-460</guid>
		<description>@Per - That&#039;s perhaps the best/easiest way to do it. I think I&#039;ll use that way from now on. I usually like to validate everything when I create an object but you can still validate everything in a function so I guess it&#039;s easier to do it like you do when you&#039;re developing with PHP.</description>
		<content:encoded><![CDATA[<p>@Per &#8211; That&#8217;s perhaps the best/easiest way to do it. I think I&#8217;ll use that way from now on. I usually like to validate everything when I create an object but you can still validate everything in a function so I guess it&#8217;s easier to do it like you do when you&#8217;re developing with PHP.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Per</title>
		<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/comment-page-1/#comment-454</link>
		<dc:creator>Per</dc:creator>
		<pubDate>Tue, 22 Jul 2008 00:40:05 +0000</pubDate>
		<guid isPermaLink="false">http://swedishfika.com/?p=71#comment-454</guid>
		<description>I prefer to create a construct without any arguments, and then create functions with different arguments. In ASP.NET you can create as many constructs you want, but PHP doesn&#039;t works like that. It&#039;s sad, but this is my opinion :)</description>
		<content:encoded><![CDATA[<p>I prefer to create a construct without any arguments, and then create functions with different arguments. In ASP.NET you can create as many constructs you want, but PHP doesn&#8217;t works like that. It&#8217;s sad, but this is my opinion <img src='http://www.swedishfika.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: woodie</title>
		<link>http://www.swedishfika.com/2008/06/06/overloading-constructors-in-php/comment-page-1/#comment-235</link>
		<dc:creator>woodie</dc:creator>
		<pubDate>Wed, 18 Jun 2008 09:44:26 +0000</pubDate>
		<guid isPermaLink="false">http://swedishfika.com/?p=71#comment-235</guid>
		<description>this is good stuff:)

keep it up!</description>
		<content:encoded><![CDATA[<p>this is good stuff:)</p>
<p>keep it up!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
