tags:

views:

106

answers:

3

I am learning about OO and classes, I have a couple of questions about OO and classes in PHP

  1. As I understand it, a class that extends another class simply means that the class that extends the other class has access to the variables/properties and the functions/methods of the class that it is extending from. Is this correct?

  2. I know that a static method or property are basically the same as a procedural function or variable outside of a class and can be used pretty much anywhere. Is this correct?

  3. Public means any class can access it and Private means only the class that is is encapsulated in or a class that is extended from the owner of can access and use. Is this correct?

+10  A: 

1) Yes, that's correct. A child class inherits any protected or public properties and methods of its parent. Anything declared private can not be used.

2) This is true. As long as the class is loaded (this goes well with your autoloading question from before), you can access static methods via the scope resolution operator (::), like this: ClassName::methodName();

3) You have the meaning of public correct, but as I mentioned earlier, private methods can only be used by the class they are declared in.

class parentClass
{
     private $x;
     public $y;
}

class childClass extends parentClass
{    
    public function __construct() {
        echo $this->x;
    }
}

$z = new childClass();

The above code will result in a NOTICE error being triggered, as $x is not accessible by childClass.

Notice: Undefined property: childClass::$x

If $x was declared protected instead, then childClass would have access. Edit: A property declared as protected is accessible by the class that declares it and by any child classes that extend it, but not to the "outside world" otherwise. It's a nice intermediate between public and private.

zombat
ok thanks for the clarification, I understand it all now except protected, I need to read up on that
jasondavis
+1 - I like the fact that you mentioned about the protected keyword. I would like to answer this question, but when you have such a fantastic answer, I figure, what is the point. <g>
James Black
Seconded, I like this answer too. I also like that @jasondavis is asking a lot of good questions about OOP.
karim79
@karim79 thanks, my project is huge and if I am going to switch it from a procedural way to use classes and such then I really need to learn this stuff ASAP before I even start the big switch
jasondavis
Just a small add-on to point 2:If using the scope resolution operator to access a class's functions, you cannot access variables or methods using $this, since the object was never initialized.
BraedenP
@BraedenP If I understab correctly it can not use "this" as you stated but you can use "self"
jasondavis
A: 

There's very little need to declare anything private, as a general rule use protected instead.

Klas
Private is a very important mechanism for data-hiding. Data-hiding enables more reliable program as you can be sure that no-one else but you (that write the class) can access the property directly.
NawaMan
I didn't say there's no use for private properties/methods, the point was that in the vast majority of cases you'll want to use protected. The more extensible your classes, the more reusable your code.
Klas
A: 

For 1. As I understand it, a class that extends another class simply means that the class that extends the other class has access to the variables/properties and the functions/methods of the class that it is extending from. Is this correct?

ANS: That is correct but that is not all. The extending class can also customize the extended class by overriding the method of the extended class. And ofcouse, it can also extend the super class functionality by adding new fields and methods.

For 2. I know that a static method or property are basically the same as a procedural function or variable outside of a class and can be used pretty much anywhere. Is this correct?

ANS: Yes that is correct and as zombat said as long as the class is public and loaded and the property and method is public. In other word, you are using the class as name space of those elements.

For 3. Public means any class can access it and Private means only the class that is is encapsulated in or a class that is extended from the owner of can access and use. Is this correct?

ANS: Think of it as physical properties, public computer (at a library) can be used by everyone and your private computer (supposibly) can only be used by you.

Just to added to Zambat comment.

NawaMan