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

Posted by mootmail-googlegroups on 08/15/06 20:54

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

It seems you are thinking of a class as nothing more than a collection
of variables and functions. The trick is that, even though that's all
they are, you aren't supposed to think of them that way. A class
represents an Object, a tangible thing with associated data and
functionality. It may just be a pile of functions, but they are
functions that, together, represent something bigger.

For example, I could reduce myself to a handful of variables and
functions. I have a name, address, occupation, and I can go to the
store to buy groceries. If I just put all of those variables and
functions out there in the main scope, then anyone can use or modify
them. But, if I instead encapuslate them in a 'matt' object, then if
anyone wants to know my name or tell me to go to the store, then they
need to go through ME to do it, which means I can control their access
appropriately. And this can be beneficial for whoever is doing the
asking, because instead of having to know who I am, where I left my car
at, and any number of other things, they just have to say
matt->buyfood(), and I'll take care of the details.

On a larger scope, if I have a Item object that represents a field in a
database, then if I want to save changes to the item, I can call
item->save(), and don't have to know that it will in turn call
db->update(foo), which will in turn call mysqldb->update(foo), which
will in turn call the php functions to interact with a mysql database.
Abstraction and Encapulation, core principles of Object-Oriented
Programming, can be very powerful.

If you really want to understand Object-Oriented Programming, there
isn't any shortage of papers/articles on the subject.

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

Neither is incorrect. Just use whatever is approriate for the project
at hand.
But, if you call a function and then have to check a property to get
the result, why not just return the result from the function in the
first place. If you need to, also set a property so that you can check
it again later. Don't make more work for yourself than necessary

>
> Of course, then I come up with the thought, that I might want to use the
> second function outside of the first function, but I've written the
> second function in such a way that it can only be called from the first
> function.

It is quite common for code to outgrow its original design. Get used
to refactoring.

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

I'm not quite sure why, but this concept bothers me. To me, a function
operates on a set of parameters and then returns a result. If I
provide those parameters at the time of the call, then I can have a
reasonable expectation that the results of the function will be
accurate. However, if I do not provide those parameters, but the
function instead gets them from some other location, then I have no way
to know if it is getting the correct values, and thus, can't trust the
return. This may not be an issue if you are the only one working on
the project, but if you want your code (and coding-style) to be
portable to a multi-developer environment, you can't trust that data
'just sitting there' isn't going to remain untouched.

 

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

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