Abstract, public etc.. Classes help.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Abstract, public etc.. Classes help.

Post 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..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Abstract, public etc.. Classes help.

Post 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.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Abstract, public etc.. Classes help.

Post 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.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Abstract, public etc.. Classes help.

Post 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.
Post Reply