Obfuscate PHP source
Requires Codefend 3.3.0 or later for the recommended m_ preset.
This PHP CLI example calculates an order total in integer cents. The application function and local variables are renamed; PHP's own functions and constants remain intact.
Install the PHP CLI and Node.js for Codefend. In a new directory, install Codefend:
npm install --global codefend1. Add the program
Create source/main.php:
<?php
declare(strict_types=1);
function m_total(int $m_price, int $m_count, int $m_percent): int
{
$m_subtotal = $m_price * $m_count;
$m_discount = intdiv($m_subtotal * $m_percent, 100);
return $m_subtotal - $m_discount;
}
echo m_total(1000, 2, 10), PHP_EOL;Expected obfuscated source
With the configuration in the next step, Codefend produces:
<?php
declare(strict_types=1);
function Ox0(int $Ox1, int $Ox2, int $Ox3): int
{
$Ox4 = $Ox1 * $Ox2;
$Ox5 = intdiv($Ox4 * $Ox3, 100);
return $Ox4 - $Ox5;
}
echo Ox0(1000, 2, 10), PHP_EOL;The program's expected output remains 1800. Generated identifier numbers can change when the source or Codefend version changes.
Only names we own use m_. The startWithMAndUnderscore preset matches the name after $ in a variable, leaving the dollar sign in place. intdiv, PHP_EOL, language keywords, and the PHP opening tag are preserved.
2. Configure Codefend
Create .codefendrc.json in the project root:
{
"generation": {
"inputDir": "source",
"outputDir": "release"
},
"parser": {
"name": "startWithMAndUnderscore"
}
}The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.
This source-only input and disposable sibling release directory keep generated files separate from originals. Codefend removes an existing output directory before generation. The default empty pool uses generated prefix-and-sequence names, and the plain main.php path does not match the preset.
Keep Composer dependencies, images, sessions, caches, and other unrelated assets outside this input tree. Codefend currently reads copied file contents as text.
3. Execute before and after
Run from the project root:
php source/main.php
codefend --obfuscate
php release/main.phpBoth versions should print:
1800The result represents two items at 1,000 cents each, less 10%. PHP documents this CLI execution form.
Integrating with an application
Preserve public class/method names, framework callbacks, route contracts, autoload paths, and fields used by serializers or templates. Test variable functions, reflection, and string-based method/property access. Named arguments are also part of a callable's external contract: changing a parameter name can affect callers.
This example keeps all function calls together and uses positional arguments. Codefend performs text replacement rather than PHP-aware symbol analysis. The preset is case-sensitive even where PHP treats names case-insensitively, so use consistent spelling for every declaration and reference. Check the current limitations and run the generated application's tests before distribution.
Add Codefend to your workflow
Place Codefend before your PHP validation and packaging steps. Lint the generated source, then run the application tests against that source so the original files do not mask a changed runtime contract.
codefend --obfuscate
php -l release/main.php
php release/main.phpIn CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.