More than one class is a mess
TL;DR: Follow the separation of concerns principle and file organization
-
Code Organization
-
Coupling
-
Autoloading problems
-
Debugging
-
Version control and merge conflicts
-
Declare a single class per file
-
Use name scoping
In languages that declare classes using a file system, having one class per file is generally considered a best practice.
This approach helps improve code organization and maintainability and reduces potential issues.
You can organize namespaces into separate directories within your project structure.
This way, you can maintain a logical and efficient codebase while avoiding the issues of declaring multiple classes in a single file.
<?
namespace MyNamespace;
class Class1 {
public function sayHello() {
echo "Hello from Class1!\n";
}
}
class Class2 {
public function sayHello() {
echo "Hello from Class2!\n";
}
}
<?
namespace MyNamespace;
class Class1 {
public function sayHello() {
echo "Hello from Class1!\n";
}
}
<?
namespace MyNamespace;
class Class2 {
public function sayHello() {
echo "Hello from Class2!\n";
}
}
[X] Automatic
Many standards enforce this rule
- Coupling
Keep your code organized and follow known standards
Code Smell 48 - Code Without Standards
Code Smells are my opinion.
Photo by Marjan Blan on Unsplash
Without requirements or design, programming is the art of adding bugs to an empty text file.
Louis Srygley
Software Engineering Great Quotes
This article is part of the CodeSmell Series.