Click to See Complete Forum and Search --> : A few questions on OOP in PHP5


Paul Jr
10-03-2004, 01:29 PM
I’ve just gotten into OOP now that I have PHP 5.0.2 installed. I’ve got a few questions.


First of all, what is class abstraction, and what are its uses?
What is the use of the static keyword? I know it makes variables and methods callable from outside object context, but why would you want to do that?
What are class interfaces? Is it basically just a way to explicitly state what methods and variables a class must implement?
Lastly, is there any way to, for lack of a better word, “extend” the functionality of the private keyword to keep a variable from being modified? I would like certain variables to be accessed only by the class in which they are defined, but I do not want to allow them to be modified.


Thanks! :)

AdamGundry
10-04-2004, 11:46 AM
This article (http://www.developer.com/lang/php/article.php/10941_3302171_2) explains things fairly well. You might also try looking for some general OOP tutorials, because these issues are by no means PHP specific - they apply to virtually every OOP language.

As regards your last question, AFAIK you can't apply access modifiers to a constant. But surely if you are declaring a private member variable, you are writing the class and would know not to change it?

Adam

Paul Jr
10-04-2004, 03:44 PM
Originally posted by AdamGundry
This article (http://www.developer.com/lang/php/article.php/10941_3302171_2) explains things fairly well. You might also try looking for some general OOP tutorials, because these issues are by no means PHP specific - they apply to virtually every OOP language.
Thanks for the link! I’ll be sure to Google for some OOP tutorials. :)

Originally posted by AdamGundry
As regards your last question, AFAIK you can't apply access modifiers to a constant. But surely if you are declaring a private member variable, you are writing the class and would know not to change it?
True, but if someone else were using it (not that anyone would want to use anything I wrote :D), I would like to take certain measures to make sure that the values of certain variables can only be set by hard coding them in when the variables are defined.

NogDog
10-04-2004, 04:00 PM
Originally posted by Paul Jr
...True, but if someone else were using it (not that anyone would want to use anything I wrote :D), I would like to take certain measures to make sure that the values of certain variables can only be set by hard coding them in when the variables are defined.
Is this what you need: http://us2.php.net/manual/en/function.define.php ?

Paul Jr
10-04-2004, 04:05 PM
Originally posted by NogDog
Is this what you need: http://us2.php.net/manual/en/function.define.php ?
I thought about using constants, but I was trying to figure out another way. I’m not exactly sure why I don’t use constants; they seem just right for what I want to do…

shimon
10-05-2004, 05:34 AM
Originally posted by Paul Jr
What are class interfaces? Is it basically just a way to explicitly state what methods and variables a class must implement?

It's almost exactly that - think of it as a contract, which promises that your class will be accessible in certain ways.

But it's limited to class functions (and then only the public ones) and it doesn't deal with variables: strictly speaking, all class variables should be private, and so are not part of the interface of a class at all. Hence adding a member variable to a PHP interface causes a Fatal error.

I think interfaces are useful for layering parts of your app: Say you have a thick DB abstraction class that implements an interface called 'DataStore', the Model or View code that uses that data would need only to know the rules of the interface, and not care about what happens inside the abstraction class.

That way you can change the abstraction class as much as you like, change it to run from Postgres, from flat text files, XML or whatever - just so long as it implements the DataStore interface. That's a somewhat idealistic example but still.

Interfaces are probably slightly less use in PHP than in a statically-typed language (Java interfaces would dictate things like the datatype of the arguments to each function, for example). But I think that where it gets exciting (if you're as sad as me ;)) is when we see more interfaces built into PHP as part of the SPL. Soon we'll have far more standard ways of doing things, and hopefully less of a big mess of code all over the place.

Harry has a nice article about the SPL here:

http://www.sitepoint.com/article/php5-standard-library

Paul Jr
10-05-2004, 02:49 PM
I think I understand a bit better now. Thank you! I’ll be sure to read the SitePoint article. ;)