Reply to Re: why use a class

Your name:

Reply:


Posted by windandwaves on 08/12/05 02:50

Peter Fox wrote:
> Following on from windandwaves's message. . .
>> Hi Folk
>>
>> I was wondering to hear from you why you use classes in some
>> instances rather than functions. I have never used a class, but I
>> use a lot of functions.
>>
>> I don't have a comp sci background, so I may miss some of the basics.
>>
>> TIA
>>
>> - Nicolaas
>>
>>
> I know where you're coming from.
>
> Look on classes and OO as a good way to do lots of things. You'll
> still use 'stand alone' functions but _thinking about_ things in an
> OO way should make a big difference. Just like your function library
> took time to develop so will your collection of handy objects.
>
> For example I have database and table classes to replace my collection
> of database related functions. A lot of the code is the same, but one
> of the things I can do now is prepare and cache certain things to
> assist performance.
>
>
> There are plenty of easy examples. See chapter 14 of the PHP manual. An
> excellent example is creating an email. There are a number of
> classes around which basically let you set a load of elements then do
> the dirty work for you.
>
>
> One example is having a user class (and working with instance
> objects). [You can think of _class_ as being the ideas that make it
> work, the design and interrelationships with other objects, and the
> _object_ as the practical realisation which gets processed.] My
> standard user class handles creating a new user, interfacing with a
> database table, managing passwords, managing permissions and change
> of email address etc. At the top of every page I can do /something/
> like
> session_start();
> $cu = GetCurrentUser(); // this is a stand alone function
> $cu.CheckPermissions('IsAdmin'); // this is an object-function
>
> The thing is I know that all my user related data and procedures are
> in the one place and the messy bits are nicely wrapped up. If
> experience shows that the user class could usefully be enriched then
> I only have to visit one bit of code to do it knowing that the
> behaviour will not be adversely affected by no-longer applicable
> functions. For example there is a class-function to validate a
> password then I thought it would be great to allow another developer
> to plug-in their own function. Not a single bit of page code had to
> be changed to make use of this feature, it was all handled 'inside'
> the class.
> There are other good reasons for adding the ability to write and use
> classes to your repertoire but it is too large a subject to go into
> here.


Hi Peter

Thank you for your in-depth answer. I am just trying to wrap my head around
it ;-)

In VB it was really nice (paradox?) when I created a class that when I
typed, for example, "myclass." then it would show all the methods for the
class (i.e.the autocomplete). However, in PHP I work in Notepad2, which
does not have that function (well, as far as I know).

Now, I have a function in a file called dropdown.php. It creates a dropdown
for any given table and field:

<?php
//creates a dropdown for a table
//$table = the name of the table
//$name = the name of the select control
//$class = the class of the select control
//$select = the ID of the item that should be selected as default
//$fieldname = the name of the field in the table that should be shown
//requires the primary key in the table to be named as ID

function dd($table, $name, $class, $select, $fieldname){
$sql = 'SELECT `ID`, `'.$fieldname.'` FROM `'.$table.'` '.$sqlw.' ORDER BY
`ID`;';
$query = mysql_query($sql);
$v = '<select name="'.$name.'" id="'.$name.'" class="'.$class.'">';
while ( $row = mysql_fetch_row($query) ) {
$v .= '<option';
if ( $select && $row[0] == $select ) {
$v .= ' SELECTED ';
}
$v .= ' VALUE="'.$row[0].'">'.$row[1].'</OPTION>';
}
$v .= "</SELECT>";
return $v;
}
?>


dont worry about the code, I am sure you get the gist. How would this
function benefit from being put into a class? I am not doubting that this
would be useful, I just trying to understand the concept better.

Thank you again for your great answer... It made it such much clearer.

[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

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