You are here: Re: Advantages of using classes over functions? « PHP Programming Language « IT news, forums, messages
Re: Advantages of using classes over functions?

Posted by Peter Fox on 05/10/05 12:11

Following on from 's message. . .
>So when should I ever use PHP classes instead. I have learned how to
>put together PHP classes but never seen a reason to use them where I
>can simply use a function.
>
>Im basically just wanting to know classes benefits over functions. If
>something can be better put together in a class I would like to be
>doing it that way. Any suggestions?
* Classes are good for when data needs to be packaged. This makes it
easier to use with sessions and do checks etc on it. So for example you
might have a user class which contains name, ID to start with plus
'embedded' functions for checking password etc. Stuff it in the session
and Bobs your uncle. Now add access permissions - (some data and some
manipulation functions) without having to touch the way the data gets
passes around behind the scenes in PHP. This also means it is ready to
run for your next project.

* If you have lots of components that get stuck together and contain
others then wrapping your data and operations up in classes means you
can deal with 'black-boxes'.

* Classes for classes sake are a pain.

* The trouble with OO is that you need to think before you code. There
is an art to this of course. IMHO you should look at the self-contained
elements for objectivising and small-scale common activities for
functionising.

* Reference documentation is possible but you need tools to do it. I
wrote my own.

* Finally you can put a huge amount of complexity into a single object
which is fantastic if you want to work with complex objects as building
blocks. Pre-fabrication. For example this is a screen with in-line
editing in a table, with standard layout features, navigation buttons,
and all database twiddles wrapped up inside somewhere. (Actually this
gets compiled into a database which a screen server picks out as
required so there is a whole lot of overhead but it means that if you
want a 100 screen system you can have it in no time flat.)

$SCREENOBJECT = new stdScreen('sutable','Suppliers','','menu01');
$SCREENOBJECT->AddBlock(new
buttonBlock(HAR,BUADD,'suedit?ID=0','Add','New supplier'));

$t=new tableElement('select SuID,SuName,SuTel,SuOurNote,SuAddress from
supplier order by SuName','No suppliers!');
$t->AddCol('Name','[SuName]');
$t->AddCol('Tel','[SuTel]');
$t->AddButton('Details',BUGO,'suedit?ID=[SuID]');
$t->AddEditArea('Address','','addr[SuID]','[SuAddress]',20,2);
$t->AddEditArea('Notes','','note[SuID]','[SuOurNote]',20,2);

$sb = new stdBlock('');
$sb->SetActiveElement($t);
$SCREENOBJECT->AddBlock($sb);


--
PETER FOX Not the same since the bottom fell out of the bucket business
peterfox@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>

 

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

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