You are here: when to use static? « PHP Programming Language « IT news, forums, messages
when to use static?

Posted by darius on 04/04/07 17:33

Hi

I started out writing a class wrapper for 3DES encryption called TDES.
It seemed logical to declare encrypt() and decrypt() methods as static,
so I can just call, e.g., TDES::encrypt("plaintext") without creating an
object. Then I wrote a class for outputting html (htmlhead() htmlfoot()
htmlform() etc.) and once again I find myself just using static functions
b/c they made sense there too. Now I'm writing a wrapper for SoapClient
and again I'm making call() and setCookie() and variousotherthings()
methods static. Is this idiotic? Do I need help? When is it proper to
use static?

Related questions:

1) If I have a static variable, the scope is only for that http request,
correct? Other invocations of the script will have a different copy of
the variable? (I'm not creating several objects of the same class in my
app.)

2) how do I initialize a static variable when I need to do more than just
set it to some constant expression. Right now I do this

class myclass {
private static myvar;
public static init() {
myclass::myvar = // whatever
}
}

myclass::init();

Slightly inelegant.

 

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

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