NathanLedet Posted October 8, 2008 Share Posted October 8, 2008 When you're working in OOP and you have a private function, what exactly is "private" about it? Quote Link to comment https://forums.phpfreaks.com/topic/127571-privatefrom-what/ Share on other sites More sharing options...
JonnoTheDev Posted October 8, 2008 Share Posted October 8, 2008 Private methods can only be called from the internals of the class. Protected methods can only be called from the internals of the class or any derived classes (sub-classes). Public methods can be called from anywhere. Without specifying anything a method or function is deemed public. Do some Googling on the topic for examples. Quote Link to comment https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-660055 Share on other sites More sharing options...
KevinM1 Posted October 8, 2008 Share Posted October 8, 2008 When you're working in OOP and you have a private function, what exactly is "private" about it? It can't be invoked by the client code. Example: class PrivateExample { private $msg = "Hello!"; public function __construct() {} private function getMsg() { return $this->msg; } } $example = new PrivateExample(); $msg = $example->getMsg(); // WON'T WORK Private functions are typically used as bookkeeping functions for the objects in which they reside. That, or in breaking down a complex public function into smaller parts that shouldn't be accessed directly by the world at large. Quote Link to comment https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-660057 Share on other sites More sharing options...
NathanLedet Posted October 8, 2008 Author Share Posted October 8, 2008 When you're working in OOP and you have a private function, what exactly is "private" about it? It can't be invoked by the client code. Example: class PrivateExample { private $msg = "Hello!"; public function __construct() {} private function getMsg() { return $this->msg; } } $example = new PrivateExample(); $msg = $example->getMsg(); // WON'T WORK Private functions are typically used as bookkeeping functions for the objects in which they reside. That, or in breaking down a complex public function into smaller parts that shouldn't be accessed directly by the world at large. OK I think I'm following you on this. In other words, private functions are functions only someone running Administrator codes can access - meaning, if I'm logged into my admin panel, then I have access to those Private functions? er am I off base here? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-660071 Share on other sites More sharing options...
KevinM1 Posted October 8, 2008 Share Posted October 8, 2008 When you're working in OOP and you have a private function, what exactly is "private" about it? It can't be invoked by the client code. Example: class PrivateExample { private $msg = "Hello!"; public function __construct() {} private function getMsg() { return $this->msg; } } $example = new PrivateExample(); $msg = $example->getMsg(); // WON'T WORK Private functions are typically used as bookkeeping functions for the objects in which they reside. That, or in breaking down a complex public function into smaller parts that shouldn't be accessed directly by the world at large. OK I think I'm following you on this. In other words, private functions are functions only someone running Administrator codes can access - meaning, if I'm logged into my admin panel, then I have access to those Private functions? er am I off base here? Thanks Way off base. It has nothing to do with site administration or authorization. We should probably start at the beginning. Objects are datastructures, much like arrays or structs (or even functions). No more, no less. They are defined by classes, which, really, is another word for datatype. So, let's define a class: class Example { public $msg = "Hi, I'm an object property!" public function __construct() { echo "I'm the object constructor. I run every time a new Example object is instantiated!"; } } Pretty straightforward. It has one property ($msg) and one method, its constructor. Now, let's create a few of these objects in our client code: $example1 = new Example(); $example2 = new Example(); Again, simple. Now, what if you wanted to access the objects' property? As it stands now, you can access it directly: echo $example1->msg; Pretty cool, right? And the same goes for overwriting that value: $example1->msg = "I have a lovely bunch of coconuts"; But therein lies the problem. Any piece of code can just change the property directly. What if you didn't want the $msg property to be changed unless you explicitly gave permission to these changes? That's where the private keyword comes in. Let's change the class to: class Example { private $msg = "I'm private now!"; public function __construct() { echo "I'm the same as before"; } public function getMsg() { return $this->msg; } public function setMsg($someText) { $this->msg = $someText; } } $msg itself is now safe from being accidentally accessed by some rogue code. It can still be accessed and changed through the getMsg and setMsg functions. Indeed, those functions force all of the code that wants to access the $msg property to do it in those particular ways, ensuring that the value isn't overwritten by accident elsewhere. The same goes for private methods/functions. Let's say each object needs to access the database upon construction: class Example { private $msg; public function __construct($id) { $initData = $this->accessDB($id); $this->msg = $initData['msg']; } private function accessDB($id) { $query = "SELECT * FROM mytable WHERE id = '$id'"; $result = mysql_query($query); $data = mysql_fetch_assoc($result); return $data; } } // .... $example = new Example(23); The accessDB method is declared as private because it shouldn't be invoked by any other code in the script. Instead, it should only be invoked when the object is constructed. Quote Link to comment https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-660104 Share on other sites More sharing options...
genericnumber1 Posted October 9, 2008 Share Posted October 9, 2008 Wow, a lot of time was spent explaining that good work. Quote Link to comment https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-660601 Share on other sites More sharing options...
NathanLedet Posted October 9, 2008 Author Share Posted October 9, 2008 wow yesss indeed great info. I really appreciate you taking the time to explain that bookmarked this thread and will be spending some time here for sure Quote Link to comment https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-661121 Share on other sites More sharing options...
Daniel0 Posted October 9, 2008 Share Posted October 9, 2008 You might want to read John Kleijn's tutorials starting with this one. Quote Link to comment https://forums.phpfreaks.com/topic/127571-privatefrom-what/#findComment-661230 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.