You are here: Re: Classes / Functions / Autonomy « PHP Programming Language « IT news, forums, messages
Re: Classes / Functions / Autonomy

Posted by Simon Dean on 08/15/06 20:45

Rik wrote:
> Simon Dean wrote:
>>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.

Ok, so, what's better to use, function parameters, or class
attributes/properties? When is the most appropriate times to use them?


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

Surely that's the idea of functions too? In that you pass data into it,
and get something returned.


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

Hrm, Im thinking of things like Database Name, Highlight Colours, I even
went anal at one point and stored a URL that would retrieve info from a
database. Why i didn't do it all in the database to begin with I don't
know. Call me lazy.

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

So I couldn't do, oh, say:

Class Parameters {
var bgColor;
var fgColor;
var dbName;

function Parameters() {
$this->bgColor="#FFFFFF";
$this->fgColor="#000000";
$this->dbName="DB123";
}
}

// and then

Class Page {
function Open() {
$Parameters = new Parameters;
OpenDatabase($Parameters->dbName);
}
}


If I can do this, then I guess Im doing something wrong, Im cheating,
and Im using something which makes all my declarations global?

Before I start doing anything major, I have a real intent to do things
the right way.

Ta
Simon

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация