Re: When to make a class
| From: | Todd Cary | Date: | Sat, 26 Nov 2005 16:54:50 +0000 |
| Subject: | Re: When to make a class | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-226477@lists.php.net to get a copy of this message | ||
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) {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... OLLiecho('<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)?