j.smith1981 Posted January 13, 2011 Share Posted January 13, 2011 Hi there (I don't know if this is the relevant part to put this thread up so I do apologise in advance). But I was just wondering, when you make a private or protected function right, it cant be called outside of the class can it? So what would you ideally use it for, then how would you call it, from another function, that passes that variable back possibly? Just a general question really to understand the indepth stuff about I suppose it would be related to encapsulation yes? Just a random question really (not going to be using them now but just curious). Thanks and I appreciate any replies in advance, Jez. Quote Link to comment https://forums.phpfreaks.com/topic/224328-protected-or-private-functions-in-oop-for-php/ Share on other sites More sharing options...
KevinM1 Posted January 13, 2011 Share Posted January 13, 2011 Private makes the data member or method accessible only to an instance of the class in which it was defined. Protected makes the data member or method accessible only to an instance of the class in which it was defined AND any child classes/objects. Neither can be invoked from the outside, so something like $obj->invokePrivateMethod(); will fail. So, in essence, yes, things marked as private or protected are for internal (to the class/object) use only. As far as uses go, well, data members should be as restricted as possible in most cases. You don't want to give the outside world direct write access to your data fields, which is what you have with public members (note that both var and no access modifier at all default to public). This is why most well-written code examples show fields marked as either private or protected. For methods, chances are you'll have classes that have functionality you don't want other objects to get their hands on, like routine db visits. Private and protected keep these parts hidden since the rest of the system really doesn't need to know how things are done in a particular object. You were on the right track with thinking it's about encapsulation. Objects should be seen as black boxes. Their outside has a public interface (public methods, not necessarily the interface language construct) which tells the world how to use it properly. The actual mechanisms that do the work are hidden to protect them. It's not like you need to know how things work inside the box to get it to fit in with others. Quote Link to comment https://forums.phpfreaks.com/topic/224328-protected-or-private-functions-in-oop-for-php/#findComment-1159013 Share on other sites More sharing options...
xylex Posted January 13, 2011 Share Posted January 13, 2011 Going a little off topic here, but you did say you wanted in depth. In addition to Nightslyr's explanation, PHP has some language specific conventions about how visibility works. DISCLAIMER - These are just things that PHP does, I certainly am not condoning them as a good programming practice to do any of the following. Protected members are also accessible to the parent class as well. So a parent method can reference a protected member that won't be defined until the child class. class A { public function test() { $this->echoOne(); } } class B extends A { protected function echoOne() { echo '1'; } } //echoes 1 Child methods can directly access protected members of an instantiated class. Continuing from above class C extends B { public function actOnFoo() { global $Foo; $Foo->echoOne(); } } $Bar = new C(); $Bar->actOnFoo(); //echoes 1 And then there's the fun one, that another instance of the same class can directly access private members of an instantiated class. This one actually can be useful if you ever need to get information about a object's private properties when you don't have access to the code, and conversely means that you shouldn't architect anything depending on visibility keywords for security. class D { private $n; public function __construct() { $this->n = 3; } public function actOnD1($newN) { global $D1; $D1->n = $newN; } public function echoN() { echo $this->n; } } $D1 = new D(); $D2 = new D(); $D2->actOnD1(5); $D1->echoN(); //echoes 5 Quote Link to comment https://forums.phpfreaks.com/topic/224328-protected-or-private-functions-in-oop-for-php/#findComment-1159108 Share on other sites More sharing options...
xylex Posted January 14, 2011 Share Posted January 14, 2011 Just realized I'm missing a step at the end of the first snippet - $Foo = new B(); $Foo->test(); Quote Link to comment https://forums.phpfreaks.com/topic/224328-protected-or-private-functions-in-oop-for-php/#findComment-1159254 Share on other sites More sharing options...
j.smith1981 Posted January 14, 2011 Author Share Posted January 14, 2011 No I know how private properties work, known that since I have been going through a particular book I have been reading. It is either protected (which can be used when extending a class yes?) or private (when they cannot be called then), but then you use either protected or private functions/methods to do things you don't want the user to access at all. Like the checking on the system say for an ISBN no from say a library or something like that, where you use say a constructor method to create a book object and then say the constructor method then calls the checkISBN method to check but then you cannot from the object call that method directly. In essence Encapsulation! Quote Link to comment https://forums.phpfreaks.com/topic/224328-protected-or-private-functions-in-oop-for-php/#findComment-1159280 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.