Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Static Class or Namespace

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).

like image 337
user1520427 Avatar asked Aug 14 '12 03:08

user1520427


2 Answers

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.

like image 96
Ja͢ck Avatar answered Sep 28 '22 10:09

Ja͢ck


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.

like image 36
tereško Avatar answered Sep 28 '22 11:09

tereško