Re: When to make a class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Olli -

A very well thought out answer. I especially liked the part, "I see the benefits of a class when functions start sharing variables.".

Todd

Oliver Grätz wrote:
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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux