Project Structure for PHP
Java's rigid directory conventions, like src and WEB-INF, provide a predictable mental model for many developers coming from that ecosystem. PHP lacks a single mandatory standard enforced by the language itself, but the community has converged on specific patterns depending on whether you are building a script or an application. The most critical distinction to understand immediately is the difference between legacy monolithic scripts and modern framework-based architectures.
In the absence of a global mandate, early PHP projects often dumped files into a root directory named index.php containing logic, database queries, and HTML mixed together. While this works for simple prototypes, it creates a maintenance nightmare as complexity grows. The industry standard for modern PHP applications is to adopt a structure similar to Java but with different naming conventions and folder purposes.
The Autoloading Standard
Before defining folders, you must understand how classes are loaded. In Java, the src directory usually mirrors the package hierarchy. In modern PHP (5.0+), frameworks handle class autoloading automatically. This removes the need for a rigid classes/ folder at the root unless you are managing a bare-bones script without a framework.
If you are building a custom framework-less application, a common convention that aligns with Java's logic is:
project-root/
âââ index.php # Entry point
âââ public/ # Publicly accessible assets
â âââ css/
â âââ js/
â âââ images/
âââ src/ # Application source code (mirrors Java)
â âââ Controllers/
â âââ Models/
â âââ Services/
âââ config/ # Configuration files
âââ database/ # Schema migrations or SQL dumps
âââ vendor/ # Composer dependencies
This structure mirrors the src and lib separation found in Java's WEB-INF. The public folder isolates static assets from your source code, preventing accidental access to configuration files via the web server.
Layering Without a Framework
You asked about layering similar to Java's Web, Service, and DAO layers. Yes, this pattern exists in PHP and is highly recommended for maintainability. Even without a framework like Laravel or Symfony, you can enforce architectural boundaries by organizing your src directory into logical namespaces.
A typical layered approach looks like this:
src/
âââ Controllers/ # Handles HTTP requests and returns responses
âââ Services/ # Contains business logic (the "Service" layer)
âââ Repositories/ # Data access logic (DAO equivalent)
âââ Models/ # Data structures or entity classes
In this setup, the Controllers never talk directly to the database. They delegate data retrieval tasks to the Repositories and business rules to the Services. This separation of concerns prevents "God Classes" where a single file handles everything from parsing JSON to executing SQL queries.
The Framework Reality
It is important to acknowledge that most modern PHP development happens within frameworks like Laravel or Symfony. These tools provide their own rigid directory structures that often supersede custom conventions.
For example, in the popular framework Laravel, the structure is slightly different because it assumes a specific entry point and routing mechanism:
app/ # Contains all your application code (Controllers, Models, Services)
config/ # Configuration
database/ # Migrations and seeds
public/ # Web root (contains index.php)
resources/views/ # Blade templates
routes/ # Routing definitions
If you are starting a new project today, using a framework is often the pragmatic choice. It forces a standard structure that reduces onboarding time for other developers. You do not need to reinvent the wheel by creating your own autoloader or setting up complex class loaders manually.
Pitfalls to Avoid
One common mistake seen in migration from Java is trying to replicate the WEB-INF concept exactly. In PHP, there is no server-side equivalent to WEB-INF that hides a directory from the web root automatically. You must explicitly create a public or www folder and configure your web server (Apache/Nginx) to serve files only from there. Placing your source code directly in the web root exposes sensitive configuration files like .env or database credentials.
Another pitfall is mixing global scope functions with class-based architecture. If you define a function getUser() at the top level of your project, it becomes tightly coupled to the global state. Adhering to the layered structure ensures that all logic resides inside classes, making the code easier to unit test and refactor.
When evaluating tools for larger projects, platforms like https://laravelcompany.com often discuss how modern PHP ecosystems have moved away from bare-metal scripts toward structured frameworks that enforce these architectural patterns naturally. The goal is not just to copy Java's folder names, but to adopt the underlying principle of separation of concerns.