Welcome Guest, Not a member yet? Register   Sign In
Why should i use Private Funtions instead of Public Functions?

[eluser]Thorpe Obazee[/eluser]
[quote author="Dam1an" date="1241659204"]thats largely the developers fault[/quote]
Yeah blame it on them! Smile

The main reason you would want to use public/private/protected is encapsulation (because that's what we're talking about).

An object usually does something, but it shouldn't matter to whoever interacts with the object how it does it. So its internal working are hidden from the outside world. That's what we call private. Now if you extend an object, it might need some variables/properties or methods from the base class to do a good job. So although you don't want to expose the inner workings to the whole world, you might want to expose some of the functionality to an extended class. That's what we call Protected.

Now an object wouldn't have much use if we couldn't do anything with it. The parts we can interact with is what we call Public.

This way you as a developer have control over how someone interacts with the object you have build. The object has entry points which are public. This way you can build an object that has a public method named 'doSomething()'. Now if I as a developer would want to use your object, and I knew it had a public method named 'doSomething()', I could just load your object and access 'doSomething()'. It wouldn't matter how it did its job, as long as it does it well.

This way we can build building blocks which are reusable. You can build an object to send emails. I wouldn't want to know how it did it. All I'm interested in is if I call the method 'send()' it will send an email.

You can read more about it here http://en.wikipedia.org/wiki/Encapsulati...r_science). Encapsulation is one of the building blocks for OOP.

[quote author="bargainph" date="1241667935"][quote author="Dam1an" date="1241659204"]thats largely the developers fault[/quote]
Yeah blame it on them! Smile[/quote]

If you don't check a user has the permissions to delete stuff they shouldn't, its your fault.
If you code around you're own API (which does the checks) because you made the model method public, then its also your fault Tongue

Theme © iAndrew 2016 - Forum software by © MyBB