1

The the PHP manual page about the exit construct states:

exit — Output a message and terminate the current script

Based on that, would it be correct to think that it violates the single responsibility principle?

Emanuil Rusev
  • 31,853
  • 50
  • 124
  • 193

2 Answers2

5

No, because exit is a procedural language construct, not a member function of any class. The single responsibility principle is supposed to apply to object-oriented programming, something which does not encompass the entire basis of the PHP language (only a portion of it).

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
2

Technically, yes. However the exact violation isn't that bad... The Single Responsibility Principle is actually an abstract principle that can be applied to any unit of code. In Steve McConnell's Code Complete 2, he actually talks about this as cohesion. In practice, this is a more specific form of the single-responsibility-principle used for routines.

The most desirable kind of cohesion according to him is functional cohesion where a routine performs one and only one operation (the examples he shows are sin(), getCustomerName(), eraseFile(), etc). So, exit() does 2 things. Therefore it shows Temporal cohesion (the operations are done in the same routine because they are done at the same time).

Now, the original arguments to exit($arg) was the return status of the applicaiton (See: linux exit status codes). In C, this is the integer value returned from main(). But since PHP doesn't have a native function, it was added in to the exit() parameter. Try it, add exit(2), and the return value of the program will be a status 2 (0 is usually success, see are there any standard linux exit status codes).

However, since PHP has a low barrier to entry, most developers likely won't know about status codes. So, it was made to accept a string. If the argument is a string, the status is echoed out on STDOUT, and then the application terminates. If it's an integer, it'll be returned from the program. So it's an artifact.

Now, is it a problem? Not really. Is it ideal? No. But it's also not horrible, since they are related. In the end, I wouldn't lose sleep over it...

Community
  • 1
  • 1
ircmaxell
  • 155,647
  • 33
  • 256
  • 309