PHP Static Class or Namespace
Asked Answered
I

2

5

I'm wanting to gauge people's opinions on the use of static classes instead of namespaces. I come from a C++ background and am quite fond of its syntax and how it lets you structure code. I recently decided I needed to group my code into logical units instead of just files. For instance I prefer calls like User::login to user_login. So, I did a bit of googling and was relieved to find that PHP has namespaces. My relief didn't last long though, I really don't like the syntax; it adds more mess to my function calls. So, at the moment I'm using static classes to simulate namespaces. Are there any downsides to this?

I found a similar question at PHP Namespaces vs Classes with static functions but there wasn't a whole lot of discussion.

Also, is there a way to avoid the following situation:

class Test {
 public static void myFunc() {
  Test::myOtherFunc();
 }
 public static void myOtherFunc() {

 }
}

I assumed it would be ok to call functions in the same class without specifying the name, but apparently not. Are there any workarounds for that (for instance in C++ there the using keyword).

Inkle answered 14/8, 2012 at 3:45 Comment(5)
self:: and $this-> are used in PHP to call your own static and instance methods respectively.Trey
So there's no way for the qualification to be implicit? ie to check the current class for a function?Inkle
Nope, but I find explicit to be a good thing :)Trey
Heh, I like it being explicit between classes but I'm used to it being implicit within a class. Oh well, guess I'll get used to it eventually.Inkle
You should just try to look beyond the aesthetic discord. It will be much more beneficial than imitating namespaces simply because you prefer the double colon.Monovalent
E
5

Coincidentally I've actually been moving into the exact opposite direction:

  1. Use namespaces to organize domain classes (or functions)
  2. Use dependency injection where I would have otherwise used static classes

The thing with static classes to simulate namespaces is that you can't organize them across multiple files, everything has to be defined inside one file; this may well be up to personal taste.

The other thing about static classes is that you start without any state and slowly some state management creeps in and you end up with some weird lock-in dependency. State should be reserved for instances. Currently my only notable static class is the site-wide configuration.

Lastly, self referencing in static classes is explicit, whereas in namespaces it works exactly like C++ would: you specify the function name and it gets looked up within the namespace first.

Eckstein answered 14/8, 2012 at 4:3 Comment(2)
I like the idea of namespaces and how they let you manage code, as you said you can organize them across multiple files. My only real gripe is the clunky syntax. I don't see the need to use \ and remove consistency with other languages.Inkle
@Inkle I know how you feel, it's not a great looker, but think of it as a directory structure thing and it makes sense :) the default auto-loader actually uses it that way too.Trey
F
4

If you look from the standpoint of code structure, there is no difference between static class method and namespaced function. They both end up in global scope. Only difference is, that, with static class method, you are trying to fake OOP.

Therefor it is better to use namespaced functions, if what you really need are standalone/utility functions. Namespaces are for grouping thing (both function and classes).

As for you User::login() example, it would be a bad practices. Instead you should have a real object, which is able to capable of containing state.

$mapper = new UserMapper;
$user = new User;
$user->setNickname( $name );

$mapper->fetch( $user );

if ( $user->hasPassword( $password ) )
{
    $user->setLastLogin( time() );
}
else
{
    // log the access attempt
    // set error state
}

$mapper->save( $user );

The bottom line is this: if you are using static structures ( functions or methods ), it is not OOP. You are just faking it. Instead you should use real OOP, with dependency injection.

If your code uses static methods and variables all over the place, it causes tight coupling between classes, add global state and makes harder to maintain and test your codebase. And this is not PHP specific.

Flavio answered 14/8, 2012 at 4:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.