Using OOP to combine multiple functions (such as a namespace) in PHP?

I work with PHP and I wonder how bad practice is to combine many functions into a class. I know that this is not the purpose of classes, but the reason why I will do this is to provide a namespace. How much can this affect the launch of, say, 10 classes when executing a PHP script instead of saying 2 or 3?

+4
source share
3 answers

If you are using php version <5.3 (and you probably cannot use namespaces) than you could use something like:

<?php class Foo { public static function aStaticMethod() { // ... } } Foo::aStaticMethod(); ?> 

(copied from php manual )

I would say that this is a class function in a sense - a grouping of functionality.

This will not have performance problems (you could do it millions of times, and you wonโ€™t even notice - there should be no time spent on launching, only a small additional cost of parsing, and this is insignificant). Modern php frameworks bring in a lot of codes and internally create a lot of objects - I would not worry about php performance, database performance almost always affects you in the first place. Make sure your code is readable and supportable (yes, especially php code;)), and if that means the grouping functions are running, then do it.

"97% of the premature optimization time is the root of all evil," especially when you make web pages, not nuclear simulations;)

Edit: public and static are only php5, in php <5 you can try:

 <?php class Foo { function aStaticMethod() { // don't touch $this } } Foo::aStaticMethod(); ?> 
+6
source

If you are so worried that loading 10 classes is too slow, you should not use PHP.

At the very least, use a cache operation code such as APC, and then loading classes should not be a burden.

+3
source

You may not be aware that PHP, recently, has support for the first class namespace: http://php.net/language.namespaces .

0
source

All Articles