PHP static class or namespace

I want to gauge people's opinions about using static classes instead of namespaces. I come from the C ++ background and really love its syntax and how it allows you to structure the code. I recently decided that I need to group code into logical units, not just files. For example, I prefer calls like User :: login to user_login. So, I did a bit of search engine work and was relieved to find that PHP has namespaces. My relief did not last long; I really dislike the syntax; this adds more clutter to my function calls. So, for now, I'm using static classes to simulate namespaces. Are there any flaws in this?

I found a similar question in PHP Namespaces vs Classes with static functions , but discussions were not discussed.

In addition, is there a way to avoid the following situation:

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

 }
}

I suggested that it would be normal to call functions in the same class without specifying a name, but apparently not. Are there any workarounds for this (for example, in C ++ there is a using keyword).

+5
source share
2 answers

By the way, I really moved in the opposite direction:

  • Use namespaces to organize domain classes (or functions)
  • Use a dependency injection in which I would use static classes

, , ; .

, - , - , - . . - .

, , , ++: .

+5

, . . , .

, / . ( , ).

User::login() , . , .

$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 );

: ( ), . . .

, , . PHP.

+4

All Articles