Page 1 of 1

Abstract, public etc.. Classes help.

Posted: Wed Feb 04, 2009 10:14 am
by Skoalbasher
Ok, I think I'm actually getting the whole OOP thing down. At least I'm understanding it better. I just need a little help here the PHP.net manual isn't giving me exactly what i'd like to know.

What the heck is abstract? Final? Protected? I don't understand these. When I looked up abstract in php.net manual, this is what I get.
Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation.

When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
What? Do all classes require a _construct function except for abstract? It's so annoying I don't know where to start. I understand them, I just dont' know how to use them. AAAHHH!!!!!

I can get books on it, but I'd rather just try and learn it, but like i've said before. If you don't already know what you're talking about, the manual does diddly. Anybody know of a website that explains them more clearly? And not just one that shows me how to draw a box on the screen..

Re: Abstract, public etc.. Classes help.

Posted: Wed Feb 04, 2009 10:33 am
by John Cartwright
The final keyword means the class or method cannot be furthur abstracted.

Code: Select all

 
final class Foo { }
class Bar extends Foo { }
Would result in a fatal error. More info can be found here.

The abstract keyword means the class or method cannot be directly instantiated, and must be abstracted.

Code: Select all

//Bad
abstract class foo { }
$foo = new foo(); 
 
//Good
abstract class foo { }
class bar extends foo { }
$bar = new bar(); //extends foo
 
More info can be found here


Public/Protected/Private are slightly different from abstract/final because they only apply the class methods and properties, and not the class object itself. More info here

Public means the method or property can be globally accessed, meaning it is not limited to the scope of the class.

Protected means the method or property can be accessed within the scope of itself, parent, or child.

Private means the method or property can only be accessed within the scope of itself.

Re: Abstract, public etc.. Classes help.

Posted: Wed Feb 04, 2009 10:46 am
by Skoalbasher
Jcart wrote:The final keyword means the class or method cannot be furthur abstracted.

Code: Select all

 
final class Foo { }
class Bar extends Foo { }
Would result in a fatal error. More info can be found here.

The abstract keyword means the class or method cannot be directly instantiated, and must be abstracted.

Code: Select all

//Bad
abstract class foo { }
$foo = new foo(); 
 
//Good
abstract class foo { }
class bar extends foo { }
$bar = new bar(); //extends foo
 
More info can be found here


Public/Protected/Private are slightly different from abstract/final because they only apply the class methods and properties, and not the class object itself. More info here

Public means the method or property can be globally accessed, meaning it is not limited to the scope of the class.

Protected means the method or property can be accessed within the scope of itself, parent, or child.

Private means the method or property can only be accessed within the scope of itself.
Ohh ok. So if you have something protected that means the child "Extends a class" and it can be called from there. But if it's private, it can only be called from the class itself. If it's in the parent, only accessable by parent, if in child, only accessable from the child.

So something is public, that means any class can call/access it, or any php script for that matter?

Believe it or not, you just helped me a TON.

Re: Abstract, public etc.. Classes help.

Posted: Wed Feb 04, 2009 1:12 pm
by mickeyunderscore
Yes that's correct, public can be accessed by anything. Generally you don't see public variables in a class.

It's worth noting that abstract classes can contain fully implemented functions. Also, by declaring a function as abstract, you force child classes to fully implement that function.