Jump to content

OOP Question


idkwhy

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.