|
Posted by Rik on 08/15/06 19:34
Simon Dean wrote:
> Hi,
>
> I have a couple of questions. If you don't mind. Sorry, I do get a bit
> wordy at times. First one just throws some thoughts around hoping for
> answers :-)
>
> 1) Anyone got a theory on the usage of PHP Classes rather than an
> actual technical guide? I've seen loads of things that show how to
> put together a class, but without actually necessarily saying why
> you'd want to use a class over say a separate file of functions or
> explaining:
> I mean, should you pass parameters into the function, or set the
> variable properties of the class? Return a value from a function, or
> set another variable?
> What are the advantages? Which way is correct?
<snip even more>
Well, using classes/objects can be a great way to group values/functions,
but it's mostly a matter of taste. It highly depends on the actual objective
wether the OO way is the way to go, or just use functional programming.
One thing I do realize after toying with OO programming for a while now:
It's a hell of a lot easier to maintain/change parts of the program, and you
don't have to keep track of your variables as religiously as in functional
programming, as they're neatly organized in an object that you can destroy
in one go, and they will never interfere with the rest of the script.
> 2) I like minimalism and trying to keep things simple as possible,
> without having to remember that in order to use a function I need to
> pass it a number of the variables from the webpage or hardcoded in my
> config.php file. ie, I want to give a function a certain amount of
> autonomy - if it wants a constant in my file, it should get it itself.
> Of course when I first started out, I was using globals.
>
> Anything better?
Constants? If they're statical all the time I'd think this is better.
Constants are freely available in functions without explicitly making them
global. Also, constants defined in functions are automatically global for
the rest of the script/classes/objects/functions.
I normally have an ini-file for different project with similar code, and at
the start of the script:
$settings = parse_ini_file("settings.ini");
foreach($settengs as $key => $value){
define($key,$value);
}
> Maybe I could just have a "SiteParameters" class, create a new
> instance, and get the information that way?
Which would result in first making this class global, then getting the value
out of it. I don't think this is such a good idea.
Grtz,
--
Rik Wasmus
[Back to original message]
|