If you have asked how to replace an old PHP system without switching it off, someone has probably answered "strangler fig". The name comes from a fig that grows around a tree, slowly takes over its place, and is still standing when the tree inside has gone. In software it means this: a new system grows around the old one and takes over its work one piece at a time, until the old one can be switched off.
Martin Fowler gave the pattern its name, and his short description is still the best starting point. This article explains what the pattern looks like in a PHP application, where it goes wrong, and when it is the wrong tool.
How it works, in four moves
- Put a switch in front of the old system. Something that looks at each request and decides whether the old code or the new code handles it. At the start, everything goes to the old code.
- Build one piece new. One page, one function, one integration. The rest of the system stays as it is.
- Move that piece's traffic to the new code. The old version stays in place for a while, so that the switch can be turned back if something is wrong.
- Remove the old piece. Once nothing uses it, delete it.
Then repeat with the next piece. At every moment the business runs on one working system, part old and part new. There is no single day on which everything changes.
What it looks like in a PHP application
Where the switch sits
There are three usual places.
At the web server. Nginx or a load balancer sends chosen addresses to the new application and everything else to the old one. A sketch:
# invoices are handled by the new application
location /invoices {
proxy_pass http://new-app;
}
# everything else stays with the old one
location / {
try_files $uri /index.php?$query_string;
}
The two applications can be on different PHP versions and different frameworks, and turning a route back is a one-line change.
Inside the application. A modern framework receives every request first. If it has a route for the address, it answers. If not, it hands the request to the old code. The Symfony documentation describes this arrangement as a legacy bridge in its guide to migrating an existing application, and the same idea works with Laravel. Both sides run in one PHP process, so the old code has to run on the PHP version the new framework needs. That is why upgrading PHP often comes first.
By job, not by address. Integrations and background work have no pages to route. Here the switch is made per flow: stock updates move to the new service this month, orders next month, returns after that.
Logins
A user who moves from an old page to a new one must stay logged in and must not notice the border. Both sides therefore have to recognise the same login, through a shared session store or a shared token. Decide how early on. It is much harder to add after several pieces have moved.
Data
The honest version: at the start, the new code nearly always reads and writes the old database. A separate database for the new part sounds cleaner, and it means copying data in both directions, which is where records start to disagree.
Two rules keep a shared database under control. For each kind of record, only one side writes it. And the new code reaches the old tables through one layer of its own, so that the odd column names and old conventions don't spread into the new code. When a piece has moved completely, its tables can be cleaned up or moved.
Retiring the old code
This is the step that gets skipped. A piece is finished when the old version has been deleted, not when the new one has gone live. Check the web server logs to confirm that nothing has called the old address for a few weeks, then remove it. Until then you are maintaining two versions of the same thing.
What to move first
- Something with clear edges. A report, a search page, one integration. It should have few connections to the rest of the system.
- Something the business will notice. The first piece has to show that the approach works.
- Not the login, and not the most tangled module. Both touch everything. Move them when the team has done two or three easier pieces and the switch has been tested in production.
Where it goes wrong
- It stops halfway. The valuable third of the system moves, the budget goes elsewhere, and the business runs two systems for good. Keep a list of every piece with its state (old, both, new) and treat "both" as unfinished work.
- Both sides write the same record. Sooner or later they disagree. If it can't be avoided for a period, decide which side is right when they differ, and check for differences automatically.
- Permissions differ. In old PHP code, the rules about who may see what are often spread across many files. If the new piece applies them differently, a user gains access they should not have. List the rules before moving a piece, and test them.
- The old side is neglected. It is still in production. It needs backups, security updates and monitoring until its last piece is deleted.
- Nobody measures. Without a count of requests to the old code, no one can say how far the work has gone.
When not to use it
- The system is small. If the whole thing can be rebuilt in a few weeks, the switch and the shared login cost more than they save.
- You don't control the routing. On some hosted platforms there is nowhere to put the switch.
- The old system is not safe to run. If it has no backups and an unsupported PHP version, deal with that first. The pattern assumes the old side can keep working for many months.
- The business process itself is being replaced. If the company is changing how it works, moving the old behaviour piece by piece carries over what you meant to leave behind.
- The parts can't be separated. Some systems are so tangled that no piece can move alone. Then the work begins with untangling inside the old code, and the pattern comes later.
The pattern doesn't require microservices either. The new side can be one ordinary Laravel or Symfony application.
Two examples
An integration replaced flow by flow. A retail group ran six Shopify stores on a custom PHP sync with SAP that failed silently, and nobody dared to touch it. We took it over and replaced it gradually with a Symfony service. The stores kept selling throughout, while the legacy code shrank.
A new interface in front of an old core. An events platform on Symfony 3.4 needed a React frontend, and its business logic was tied to old page templates. We did not rewrite it. We exposed the existing logic through more than 20 REST endpoints. The old pages stayed in production while the new interface was built against the API, and bookings were not interrupted. Only the interface was replaced. The business logic stayed where it was, which is often the right place to stop.
What to ask for if you are paying for it
- A list of the pieces, in the order they will move, and why that order.
- For each piece: how the switch is turned back if the new version fails.
- A monthly figure for how much traffic still reaches the old code.
- The date each finished piece was deleted from the old system.
- What keeps the old side patched and backed up in the meantime.
Short answers
Is the strangler fig pattern the same as refactoring?
No. Refactoring improves code inside the existing system. The strangler fig pattern builds a replacement beside it and moves the work across. Many projects use both: refactor what is worth keeping, replace what is not.
How long does it take?
It depends on how many pieces there are and how tangled they are. The first piece takes longest, because the switch, the shared login and the data rules are built with it. Later pieces go faster.
Do we have to finish?
No, but decide it on purpose. Some old parts work, change rarely and cost little to keep. Leaving them is a reasonable decision. Leaving them because the project ran out of attention is not.
Where we fit
We use this pattern in legacy PHP modernisation when a module has to be replaced and not just improved, as one part of modernisation without a complete rewrite. For a broader view of replacing old systems in stages, Martin Fowler's site has a longer series, Patterns of Legacy Displacement.
Whether the pattern suits your system depends on what is in it. A System Risk Check gives you a written picture of the application and its risks at a fixed price, and we stay responsible for the result through ongoing PHP application maintenance.





