idkwhy Posted September 2, 2012 Share Posted September 2, 2012 Hey everyone. I posted here a few months back and received great help and assistance. Thank you so much, you guys are great. Now I have a question about another tutorial I'm currently reading. Line 5 defines the Method 'add', which is a Public Method. You will recall from part one, that methods are simply functions within a class, and that Methods can either have a public, protected or private accessor defined with it. This will mean that if the accessor is Public, we can access it from anywhere outside of the class. If it is Protected, we can access it in any class that extends or is extended by the class, and if it is private we can access it only directly from within the class that it is defined in. Luckily for us, this Method is public, and can be accesses anywhere. We also see that two arguments are passed into the Method Add, namely $a and $b. This works exactly the same as a normal PHP Function, and in this case we are passing in two values that we would like to add together. You can view the tutorial here: http://www.htmlgoodies.com/beyond/php/object-oriented-programming-in-php-class-2-uses-of-objects.html I have a few questions about this paragraph. Does public mean you can access it from any file on the directory of your server? So if you reference it in another file, can it be used? I don't understand what protected means at all.. Can someone give me a rough example? Private means it can only be used in the same file / code it's being called in, correct? Please correct me if I'm wrong Thanks everyone in advance! Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 2, 2012 Share Posted September 2, 2012 It's nothing to do with files, and has to do with the classes/objects. In that example, if you create a calculator object: You could do $calculator = new calculator(); $calculator->add(); If it's private, you could only call add() from within the object, so it would be like you'd create another method which WAS public, and when you called that, that method did $this->add(); Protected means if you extend the class by having another class like Class myCalc extends calculator{} That class could do $this->add() but you still can't do $calculator->add(); If you still don't get it, try using their example, and running some code with it, and changing private/protected/public to see what you can do with them. Quote Link to comment Share on other sites More sharing options...
idkwhy Posted September 2, 2012 Author Share Posted September 2, 2012 Thank you so much jesirose for your time replying I think I understand what you mean. I want to try a hands on example with the code he provided in the tutorial to make sure I understand it.. but I'd like to ask you one more question before I do that. Say I needed to reference the code in another file.. How would I do that? Would that be more involved with MySQL or another function / method? Sorry if I'm totally off.. I don't have much experience D: Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 2, 2012 Share Posted September 2, 2012 You include the file, I always use require_once It has nothing to do with MySQL - that's a database. Look for some much more basic tutorials Quote Link to comment Share on other sites More sharing options...
idkwhy Posted September 3, 2012 Author Share Posted September 3, 2012 Thank you I understood the tutorial, I'm just learning different ways to do things.. I wanted to make sure I didn't have to refer to a database or another function / method. Thanks for giving me direction there Quote Link to comment 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.