Reply to Re: Classes / Functions / Autonomy

Your name:

Reply:


Posted by Jerry Stuckle on 08/15/06 23:30

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:
>
> Class someclass {
> var $WhyUseVars;
> var $Something;
>
> function somefunction ($OrParameters) {
> $this->Something = result;
> return $OrReturnHere
> }
> }
>
> 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?
>
> I start thinking, if you start using properties to pass information in
> and out, it's going to get very complicated and messy with properties
> that have no real useful purpose.
>
> But then if you have one function that relies on another function, there
> is the danger of getting variables floating around from one place to the
> other.
>
> eg, I pass in a code to function1. Function1 doesn't use that code, but
> passes it onto Function2 in order to perform a lookup and return
> readable information to Function1.
>
> 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.
>
> 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?
>
> Maybe I could just have a "SiteParameters" class, create a new instance,
> and get the information that way?
>
> Im not sure what the best way is, I know I just want to avoid needless
> repetition, keep things simple, and avoid the situation where Im passing
> the same information in chained function calls.
>
> Cheers
> simon

Simon,

In addition to what the others have said, let me give you another example.

As others have said, a class represents an object. An object has state
(one or more values) and operations you can perform on it.

For instance - at its most basic level, a variable can be considered a
type of an object. Take an int for example. It contains one value,
and you can perform operations on it such as add, subtract, etc.

A string can also be considered another type ofan object - there you
have additional functions such as strlen(), strcpy(), etc. No, the
syntax isn't the same as for a user-defined class. But this is very
simplistic.

A floating point number is even more complex. Typically floats are
stored internally as a number (mantissa) times some power of 2 (base).
For instance, 40 would be stored as 5,3. That's decoded as 5 times 2 to
the 3rd power (8). Adding floating point numbers can easily get
complicated. But the system takes care of all of that for you, so you
don't have to worry about the internal representation.

A PHP class is just a user defined object. The difference here is you
define the state and the operations. The variables in the class are
meant to hold permanent state - that is, state which extends beyond a
single member function call, just like the value of an int variable
extends beyond an arithmetic operation, or the value of a string lasts
past the strlen() call.

mootmail's example of a database field is a good one. The variables in
the class would contain the data from the database. It might be filled
in by a fetch() call, for instance, and you'll want to keep the data in
the object so you don't have to someplace else.

At some future time if you want to change the 'name' value, you can call
setName() function in the class. And if you want this to be saved in
the database, you can call update(). The class will take care of
everything else and you don't have to worry about it- just like you
don't need to worry about the internal representation of a floating
point value.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original 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

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