<?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: Good use of public, private and protected in OO class design</title>
	<atom:link href="http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=good-use-of-public-private-and-protected-in-oo-class-design</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Wed, 03 Aug 2011 06:49:15 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Jesse</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1661</link>
		<dc:creator>Jesse</dc:creator>
		<pubDate>Wed, 21 Jul 2010 01:42:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1661</guid>
		<description>I don&#039;t think anybody has mentioned the need for a factory. If you want to keep the api simple for a webservice for example, much of the web service description could be reflected via a Factory class, which can be used in simplifying constructors for sub-classes (1 less argument), and ultimately don&#039;t have access to the object being created by the factory because it is private.. fair enough ??</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think anybody has mentioned the need for a factory. If you want to keep the api simple for a webservice for example, much of the web service description could be reflected via a Factory class, which can be used in simplifying constructors for sub-classes (1 less argument), and ultimately don&#8217;t have access to the object being created by the factory because it is private.. fair enough ??</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JW Eshuis</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1660</link>
		<dc:creator>JW Eshuis</dc:creator>
		<pubDate>Tue, 20 Jul 2010 18:19:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1660</guid>
		<description>I agree on Ivo. I think the solutions he describes is the best solution for this problem. If indeed Symphony (and sorry @stefan i don&#039;t know it that well) only uses public or protected functions i think they will have a problem with backwards compatibility in the future.</description>
		<content:encoded><![CDATA[<p>I agree on Ivo. I think the solutions he describes is the best solution for this problem. If indeed Symphony (and sorry @stefan i don&#8217;t know it that well) only uses public or protected functions i think they will have a problem with backwards compatibility in the future.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FredTheSwiss</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1659</link>
		<dc:creator>FredTheSwiss</dc:creator>
		<pubDate>Tue, 20 Jul 2010 14:23:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1659</guid>
		<description>(Please do not take into account my previous and wrong message).

Private methods are often the symptom of breaking a simple rule of thumb: if your class description contains an &quot;AND&quot;, your class does too much and should be broken.

It is the case of your example: The account class record the data and validate it and publish the events.

Public, private, protected has more to do with writing clean code than source code contract.</description>
		<content:encoded><![CDATA[<p>(Please do not take into account my previous and wrong message).</p>
<p>Private methods are often the symptom of breaking a simple rule of thumb: if your class description contains an &#8220;AND&#8221;, your class does too much and should be broken.</p>
<p>It is the case of your example: The account class record the data and validate it and publish the events.</p>
<p>Public, private, protected has more to do with writing clean code than source code contract.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FredTheSwiss</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1658</link>
		<dc:creator>FredTheSwiss</dc:creator>
		<pubDate>Tue, 20 Jul 2010 14:16:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1658</guid>
		<description>Private methods are often the symptom of breaking the Demeter Law (Which is the case of your example).

Public, private, protected has more to do with writing clean code than source code contract.</description>
		<content:encoded><![CDATA[<p>Private methods are often the symptom of breaking the Demeter Law (Which is the case of your example).</p>
<p>Public, private, protected has more to do with writing clean code than source code contract.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Seva Lapsha</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1657</link>
		<dc:creator>Seva Lapsha</dc:creator>
		<pubDate>Mon, 19 Jul 2010 22:01:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1657</guid>
		<description>What scares me out is that these points come to discussion at all. It just clarifies about level of expertise of these people in programming at all.

First they dismiss static, then access levels... What will come next? Constructors? :) :(</description>
		<content:encoded><![CDATA[<p>What scares me out is that these points come to discussion at all. It just clarifies about level of expertise of these people in programming at all.</p>
<p>First they dismiss static, then access levels&#8230; What will come next? Constructors? <img src='http://www.jansch.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  <img src='http://www.jansch.nl/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Koen</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1656</link>
		<dc:creator>Koen</dc:creator>
		<pubDate>Mon, 19 Jul 2010 19:14:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1656</guid>
		<description>Protected methods should be forbidden in open source projects. Only public and private.</description>
		<content:encoded><![CDATA[<p>Protected methods should be forbidden in open source projects. Only public and private.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gargoyle</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1655</link>
		<dc:creator>Gargoyle</dc:creator>
		<pubDate>Mon, 19 Jul 2010 19:08:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1655</guid>
		<description>Good post Ivo. I missed the original discussion, but I agree with you. I have seen a lot of examples recently in the area of database mappers and models that are implementing public __call()/__get()/__set() methods that basically end up providing a means to access private properties - thus completely removing the encapsulation!

For those that are arguing that they cannot change the database and override  __updateRecord, well that&#039;s exactly what is intended! If you change the database then you are responsible for overriding _updateBalance and providing your own equivalent of the private _updateRecord() method that &quot;knows&quot; about your version of the database.

As an example, lets assume I wanted a version of account that stored 15% of the deposits as &#039;sales commission&#039;, then I would override _updateBalance() that split off the 15%. I would then call parent::_updateBalance() with the remaining 85%, and write my own _updateCommissionRecord() that knows where to save my custom database value!</description>
		<content:encoded><![CDATA[<p>Good post Ivo. I missed the original discussion, but I agree with you. I have seen a lot of examples recently in the area of database mappers and models that are implementing public __call()/__get()/__set() methods that basically end up providing a means to access private properties &#8211; thus completely removing the encapsulation!</p>
<p>For those that are arguing that they cannot change the database and override  __updateRecord, well that&#8217;s exactly what is intended! If you change the database then you are responsible for overriding _updateBalance and providing your own equivalent of the private _updateRecord() method that &#8220;knows&#8221; about your version of the database.</p>
<p>As an example, lets assume I wanted a version of account that stored 15% of the deposits as &#8216;sales commission&#8217;, then I would override _updateBalance() that split off the 15%. I would then call parent::_updateBalance() with the remaining 85%, and write my own _updateCommissionRecord() that knows where to save my custom database value!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cory Comer</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1654</link>
		<dc:creator>Cory Comer</dc:creator>
		<pubDate>Mon, 19 Jul 2010 18:59:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1654</guid>
		<description>You have a bank, a teller, and a customer. A customer walks up to the teller... &quot;Hello, I have this check here for $400 I would like to deposit into my bank account, here is my account number.&quot;. The teller takes the check, looks at the back of the check to ensure it has been endorsed, maybe looks for a watermark or some other means of verifying authenticity, punches the account number into the computer and then runs the check through a little machine that scans it into the bank&#039;s computer. The machine, owned and operated by the bank checks the serial number on the check to ensure it is correct, verifies the account number, and then sets up the withdraw and deposit. The teller hands the customer a receipt, all is right with the world. 

The teller has methods that are considered public, any customer can access them. They are abstractions and aggregates of other functionality the teller must perform in order to satisfy the expectations of the customer. The depositMoney method for example makes a call to the banks protected method verifyCheck to ensure the check has been endorsed and is in fact real. Once the teller has satisfied it&#039;s own inherited prerequisites for the check to be deposited, they put it into the machine that the bank manages to initiate the withdraw and deposit. The teller does not know how this works in the same sense that the customer doesn&#039;t know all the details of the verification process the teller performs. 

The bank will need to perform many actions to setup the withdraw and deposit, and these actions will no doubt change in the future. But the teller need only pass the check to the private initiateWithdrawAndDeposit method of the bank to initiate this process. The teller may incorporate their own verification methods depending on their training and expertise to ensure the check isn&#039;t fraudulent and the customer is following the banks standards and requirements.

In the end, the customer is only exposed to the functionality they need, in this example actually depositing a check. The teller is given discretion as to how the verification of the check happens, and the bank dictates the deposit process without exposing it to teller or customer. 

Public, protected, and private. 

Composition and encapsulation are core tenets of OOP. In this scenario, if everything had been public, and the customer had invoked a particular method of withdraw and deposit, or if the teller had done this the bank would be forced to always support that particular method for backwards compatibility. Maintaining backwards compatibility becomes an exponential liability as eventually there comes a time compatibility is usually broken, and either refactoring starts to creep in or you end up with project forking and divergent code bases.</description>
		<content:encoded><![CDATA[<p>You have a bank, a teller, and a customer. A customer walks up to the teller&#8230; &#8220;Hello, I have this check here for $400 I would like to deposit into my bank account, here is my account number.&#8221;. The teller takes the check, looks at the back of the check to ensure it has been endorsed, maybe looks for a watermark or some other means of verifying authenticity, punches the account number into the computer and then runs the check through a little machine that scans it into the bank&#8217;s computer. The machine, owned and operated by the bank checks the serial number on the check to ensure it is correct, verifies the account number, and then sets up the withdraw and deposit. The teller hands the customer a receipt, all is right with the world. </p>
<p>The teller has methods that are considered public, any customer can access them. They are abstractions and aggregates of other functionality the teller must perform in order to satisfy the expectations of the customer. The depositMoney method for example makes a call to the banks protected method verifyCheck to ensure the check has been endorsed and is in fact real. Once the teller has satisfied it&#8217;s own inherited prerequisites for the check to be deposited, they put it into the machine that the bank manages to initiate the withdraw and deposit. The teller does not know how this works in the same sense that the customer doesn&#8217;t know all the details of the verification process the teller performs. </p>
<p>The bank will need to perform many actions to setup the withdraw and deposit, and these actions will no doubt change in the future. But the teller need only pass the check to the private initiateWithdrawAndDeposit method of the bank to initiate this process. The teller may incorporate their own verification methods depending on their training and expertise to ensure the check isn&#8217;t fraudulent and the customer is following the banks standards and requirements.</p>
<p>In the end, the customer is only exposed to the functionality they need, in this example actually depositing a check. The teller is given discretion as to how the verification of the check happens, and the bank dictates the deposit process without exposing it to teller or customer. </p>
<p>Public, protected, and private. </p>
<p>Composition and encapsulation are core tenets of OOP. In this scenario, if everything had been public, and the customer had invoked a particular method of withdraw and deposit, or if the teller had done this the bank would be forced to always support that particular method for backwards compatibility. Maintaining backwards compatibility becomes an exponential liability as eventually there comes a time compatibility is usually broken, and either refactoring starts to creep in or you end up with project forking and divergent code bases.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eRadical</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1653</link>
		<dc:creator>eRadical</dc:creator>
		<pubDate>Mon, 19 Jul 2010 18:43:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1653</guid>
		<description>I agree with Ivo... access modifiers are so unused and/or misunderstood in PHP world...

And for those asking for &quot;different backend&quot;  can look at &quot;Strategy Pattern&quot; (http://en.wikipedia.org/wiki/Strategy_pattern).</description>
		<content:encoded><![CDATA[<p>I agree with Ivo&#8230; access modifiers are so unused and/or misunderstood in PHP world&#8230;</p>
<p>And for those asking for &#8220;different backend&#8221;  can look at &#8220;Strategy Pattern&#8221; (<a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow">http://en.wikipedia.org/wiki/Strategy_pattern</a>).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robin Speekenbrink</title>
		<link>http://www.jansch.nl/2010/07/19/good-use-of-public-private-and-protected-in-oo-class-design/#comment-1652</link>
		<dc:creator>Robin Speekenbrink</dc:creator>
		<pubDate>Mon, 19 Jul 2010 18:27:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.jansch.nl/?p=3261#comment-1652</guid>
		<description>hmmmzz... been following the discussion on the side, but Ivo, you clearly know how to simplify a delicate situation :) So please allow me to add in my 2 cents...

In your example, i could redeclare (overload if you will) the _updateBalance() and not call the parent, but just call my own definition of __updateBalance (or __overloadedUpdateBalance, ___updateBalance whatever) just by having it private is by no means a &#039;hard&#039; lock in that every one of your derivatives will adhere to the given standard... 

I do agree with you that some form of bottom line should be defined, but that should be defined on a per-project-basis... This shouldn&#039;t be a discussion on _all_ PHP projects should do this or that... but MY project does this... not that.

Again that also goes for the point of &#039;forking&#039; instead of &#039;hacking&#039;: thats just bypassing the whole discussion. The choice of VCS or the fact that the project is OpenSource or not, has IMHO nothing to do with it... It is about the choices made by the original developer of what contract is available to you as user/developer of the application. The use is to be documented along with usecases and examples. The fact that you can peek inside the code is a plus, the fact that you can alter the actual code is just room for disaster. Forking could ba solution, but has long term issues. Just live by the contract or if you really have to change it, overload the function and stick to it...  This exact problem is why the great thinkers of the past thought up the programming by contract paradigm. Define the contract, extend what you like, but keep the contract..

The argument of unittesting seems trivial: the given example is an ideal candidate for stubbing/mocking and via dependency injection so that every part (including the privates of sending out events) could (and should be btw) easily be tested

The fact remains: symfony made a decision. Any decision should be adhered when using the framework, just like their choice for a certain class signature, this is just part of the project. Live with it or live without it.. It&#039;s just that simple... (or am I now the one over simplifying it...?)

Oh and for the record, i am not a true symfony user, though a great fan of Mr. Potencier&#039;s work! I will therefore be looking forward to his say in this... :)</description>
		<content:encoded><![CDATA[<p>hmmmzz&#8230; been following the discussion on the side, but Ivo, you clearly know how to simplify a delicate situation <img src='http://www.jansch.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So please allow me to add in my 2 cents&#8230;</p>
<p>In your example, i could redeclare (overload if you will) the _updateBalance() and not call the parent, but just call my own definition of __updateBalance (or __overloadedUpdateBalance, ___updateBalance whatever) just by having it private is by no means a &#8216;hard&#8217; lock in that every one of your derivatives will adhere to the given standard&#8230; </p>
<p>I do agree with you that some form of bottom line should be defined, but that should be defined on a per-project-basis&#8230; This shouldn&#8217;t be a discussion on _all_ PHP projects should do this or that&#8230; but MY project does this&#8230; not that.</p>
<p>Again that also goes for the point of &#8216;forking&#8217; instead of &#8216;hacking&#8217;: thats just bypassing the whole discussion. The choice of VCS or the fact that the project is OpenSource or not, has IMHO nothing to do with it&#8230; It is about the choices made by the original developer of what contract is available to you as user/developer of the application. The use is to be documented along with usecases and examples. The fact that you can peek inside the code is a plus, the fact that you can alter the actual code is just room for disaster. Forking could ba solution, but has long term issues. Just live by the contract or if you really have to change it, overload the function and stick to it&#8230;  This exact problem is why the great thinkers of the past thought up the programming by contract paradigm. Define the contract, extend what you like, but keep the contract..</p>
<p>The argument of unittesting seems trivial: the given example is an ideal candidate for stubbing/mocking and via dependency injection so that every part (including the privates of sending out events) could (and should be btw) easily be tested</p>
<p>The fact remains: symfony made a decision. Any decision should be adhered when using the framework, just like their choice for a certain class signature, this is just part of the project. Live with it or live without it.. It&#8217;s just that simple&#8230; (or am I now the one over simplifying it&#8230;?)</p>
<p>Oh and for the record, i am not a true symfony user, though a great fan of Mr. Potencier&#8217;s work! I will therefore be looking forward to his say in this&#8230; <img src='http://www.jansch.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>

