Comments on: visibility and inheritance. https://phpprotip.com/2009/08/visibility-and-inheritance/ Together we can defeat the internet Mon, 27 Jun 2016 06:38:41 +0000 hourly 1 https://wordpress.org/?v=4.7.33 By: Fake51 https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-79 Thu, 13 Aug 2009 10:35:08 +0000 http://phpprotip.com/?p=164#comment-79 Looking at the documentation for visibility in PHP it seems the point is exactly visibility – i.e. having virtual functions in parent and child class means that both of them can safely call the method + you can change behaviour as needed.

Apart from that, I stick to using protected functions mainly, only exposing the functions that should be called outside a class. One of the main reasons for this is the experience from working in an international organisation on a volunteer basis – everyone comes from a different background, with different experiences, and there’s a high chance that you will see some code used in ways you hadn’t quite planned. If this leads to tight coupling (and it almost certainly will), then you’re headed down the nightmare path.

The more public methods a class has, the more trouble you’re looking at in terms of maintenance when you have to update the class or switch it out.

Regards
Fake

]]>
By: Chance Garcia’s Blog: Visibility and inheritance | PHP https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-73 Wed, 12 Aug 2009 16:03:26 +0000 http://phpprotip.com/?p=164#comment-73 […] this recent post to his blog Chance Garcia looks at visibility and inheritance in PHP applications. Specifically, it […]

]]>
By: Chance Garcia’s Blog: Visibility and inheritance | Webs Developer https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-71 Wed, 12 Aug 2009 15:00:53 +0000 http://phpprotip.com/?p=164#comment-71 […] this recent post to his blog Chance Garcia looks at visibility and inheritance in PHP applications. Specifically, it […]

]]>
By: Chuck Burgess https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-69 Wed, 12 Aug 2009 14:20:14 +0000 http://phpprotip.com/?p=164#comment-69 I choose to use protected methods by default, and go to public only for methods that I implement to follow the interface I spec’d out. In choosing how I architect my interface, I’m choosing how I’ve modeled it to be USED. How it does its own work, that’s in the protected methods.

When it comes to testing, I always prepare a mock object for each class, at least so that future tests that need a mock of the given class will have one in place that was built while the new class was still fresh in my mind. Its mocked public methods are designed to return controlled responses, just like you’d want from a good mock object. In addition, I’ll create public wrapper methods that wrap around the original class’s protected methods, thereby making each protected method testable by itself. This isn’t about code coverage or the API… it’s about me being more thorough in guaranteeing the behavior of my protected methods.

By segregating logic into small units that each “do one thing and do it well”, I typically end up with many protected methods. By trying to keep my API as clean as I can, and true to my architectural intent, I limit the amount of public methods. Tests against my public methods still exercise the protected methods that they use, true enough. My insistence on the mocked object’s public wrappers allowing me to directly test the protected methods is just additional insurance, done in a way that I can more closely target each protected method’s behavior.

I’ll rarely if ever choose private for anything. Nearly all code I write is done with the expectation that it is or could be reused elsewhere… as such, I trust to protected methods for allowing child classes to do what they need to do, whether it’s decorating the parent’s protected methods or overriding them completely.

I was actually *very* surprised to see your example of a private parent method being overridden by a child class. Did the #phpc discussion touch on this being a visibility enforcement bug?

]]>
By: Daniel O'Connor https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-47 Mon, 10 Aug 2009 08:57:59 +0000 http://phpprotip.com/?p=164#comment-47 Public, public for all!

I take the view of if you are creating code, you are writing a test for it, and you make the test pass with the barest of implementations.

You come back the next day, and realise you are repeating the same pattern of checking input in a number of places – ie, a validate().

This was a hidden design problem, so you decide to implement a protected method and shift the common code up.

However, when you really think about it, shifting a method up because of a hidden design problem really means you haven’t modelled it properly; and your only real option from here is to make it another class.

That way you can break the responsibility from your first object into another object, and increase the specialisation of each.

The payoff for creating a whole new class might be minimal if all it does is validate() though; so usually I apply my Best Judgement, and simple expose the method as public / write test coverage of it, with a @todo to refactor it elsewhere or add it to an interface if it seems common enough.

Doing a Bad Thing once is unpleasant, twice is ugly, three times is a good time to refactor.

]]>
By: Gaetano https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-46 Mon, 10 Aug 2009 08:09:47 +0000 http://phpprotip.com/?p=164#comment-46 When i started coding php, one of the big gestatlt moments I had is when i realized how much time I was saving by NOT having to think about private/protected/public methods in my applications – and how much time I had wasted before for no gain at all. That was php4 time of course, and all methods where public. Just use an underscore for methods ‘meant’ to be private, and you can have a nice api with private separate from public and do unit tests at the same time. Enforcing ‘private’ at the compiler level gains you very little, as if you have a coder in your team that breaks the convention he’s either a moron that’d benefit of some training or a guru who’s more effective at breaking standards than you would be at following them.
This of course applies to applications, not frameworks, where the code will be reused for ages in different contexts, and it benefits of course of the php execution model of wipe-everything-at-end-of-page…

]]>
By: noel darlow https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-36 Sat, 08 Aug 2009 20:22:11 +0000 http://phpprotip.com/?p=164#comment-36 I think there must be some kind of misunderstanding. I didn’t intend to post spam to your blog. There is a link to a piece about visibility in php which I thought might be relevant to your question (private is evil) but I don’t actually have a rival unit test library to promote. I have written some extensions for simpletest – which I didn’t actually mention in my post – and they are available on sourceforge but I don’t really “plug” them because they’re too tuned in to my own specific needs (for example no windows support at present). They’re there, and the rest of the library, to share ideas and to promote testing.

I like to help people learn about testing, if I can, and I tried to help you. I don’t understand why you should feel offended.

]]>
By: Les https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-34 Sat, 08 Aug 2009 16:37:34 +0000 http://phpprotip.com/?p=164#comment-34 > …then you’re better off using final.

Exactly but you have got to be careful and not just plaster it all over the place.

The way I look at it is “public”, “private”, and “protected” provide an aid for documentation and a guide to the unwary developer as to the intended wishes of the original developer(s).

And who knows? A future version of PHP may actually fix any current issues and we do have the expected behaviour.

> Personally, I never use private or…

Personally I wish you would stop plugging your silly little testing library all over the web.

I agree TDD is important but there are already far better, more mature tools out there that do the same job… but only better.

Be gone with you McGuff.

]]>
By: noel darlow https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-31 Fri, 07 Aug 2009 23:13:20 +0000 http://phpprotip.com/?p=164#comment-31 You shouldn’t really think about implementation – the non-public bits – while you’re writing tests, just interfaces and behaviours. Think about what the client expects to happen when a button is pressed or a lever is pulled and describe that in a test. Keep adding new fixtures (and tests) until you’ve covered everything you need to cover. Any private/protected bits which don’t get exercised by all can be deleted: they’re not required. It helps if you write tests first and then code.

Mocks are intended to expose internal workings but only so you can “discover” the neighbouring objects in the design. TDD is primarily about design rather than testing – although that too.

Personally, I never use private or protected. They don’t actually do anything useful. http://aperiplus.sourceforge.net/visibility.php

]]>
By: chance https://phpprotip.com/2009/08/visibility-and-inheritance/comment-page-1/#comment-29 Fri, 07 Aug 2009 18:34:46 +0000 http://phpprotip.com/?p=164#comment-29 @ingorenner pointed out to me on twitter that “It’s more important to assure that the public API is working. How internals work – thus private – is up to you”

And he also pointed out that I had lame register and login to comment on. I’ll have to look into captcha for spam filtering but think my akismet should get it. Guess I’ll find out. Anyways, the thing I have with that statement is mostly likely due to my nubbish skills as a programmer and developer. So let me explain part of how I’ve been developing with tests.

If a public function I have contains a private function. Then while test/debugging, how do make sure that the private function or functions aren’t part of a logic error that I’m trying to debug.

The root of my issue with private functions and testing may be due to the fact that some of my function do contain more than 1 private function that may be the cause of a test failing. Having a test for each function called within another function has been a benefit of unit testing for me.

]]>