You are here: Re: When to make a class « PHP « IT news, forums, messages
Re: When to make a class

Posted by Oliver Grδtz on 11/26/05 18:32

Todd Cary schrieb:
> My background is in Object Oriented Pascal (Delphi), however I am having
> difficulty knowing when to make a class in PHP. For instance, in my
> script file, functions.php, I have these functions among others:
>
> /* Input a field */
> function input_field($name, $value, $size, $max) {
> echo('<INPUT TYPE="text" NAME="' . $name . '" VALUE="' . $value .
> '" SIZE="' . $size . '" MAXLENGTH="' . $max . '">');
> };
>
> /* Input a password field */
> function input_password_field($name, $value, $size, $max) {
> echo('<INPUT TYPE="password" NAME="' . $name . '" VALUE="' . $value .
> '" SIZE="' . $size . '" MAXLENGTH="' . $max . '">');
> };
>
>
> Should I have a class that contains these functions (methods)?

Simple answer:
If YOU don't see the benefits of a class then you shouldn't use one.

Longer answer:
I see the benefits of a class when functions start sharing variables.
Then you would have to use $GLOBALS or the global keyword. This is a big
indication that the functions should instead be inside a class and that
the variables should be properties of that class.
Of course, when you start using classes you slowly move to using classes
even if they're not absolutely necessary as with your two functions up
there. Then one notices that there are even more types of input fields
(radio buttons, hidden fields, select boxes...) and that they have
something in common (attributes of the input tag). So one could think
"Hey, why not make a base class for all fields?". And so on...

OLLi

 

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

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация