It is possible that issues may arise when using obfuscation in your encoding settings. The most common symptoms of these sorts of issues are changes in code behaviour or code just crashing from references to undefined variables or functions.
That’s why in this tutorial, we will be looking at a few causes of these types of issues and how to resolve them.
What is Obfuscation?
The basic idea of obfuscation is that it changes the names of functions, variables and classes. This makes understanding the code more difficult, however errors can arise if code attempts to reference the original names. The scenarios and solutions can vary and depend on how your PHP code is written or what framework you are using.
Potential Causes of Issues
Function names passed as strings
A common cause of errors is when function names are stored in and used from variables, such as when defining CodeIgniter hooks. Looking at the following code, you can see class and function names being specified as strings:
$hook['pre_controller'] = array( 'class' => 'PreControllerClass', 'function' => 'preControllerFunction', 'filename' => 'Class.PreController.php', 'filepath' => 'hooks' );
This would work without obfuscation because the class and function names are unchanged. If the names are changed by obfuscation then attempts to reference the original names will fail as they do not exist.
The eval() function
This function is another cause of obfuscation related issues. The following piece of code will fail because it references a variable that no longer exists:
<?php $testVal = 5; $booleanVal = eval('return ($testVal === 5);'); ?>
In contrast, the following code will work because everything the eval() function needs is self-contained.
<?php $booleanVal = eval('$testVal = 5; return ($testVal === 5);'); ?>
Obfuscation Exclusion Lists
An obfuscation exclusion list specifies a set of class, method or function names that are to be excluded from being obfuscated so that they can be referred to by their original names.
Using the CodeIgniter hooks example, which fails as CodeIgniter is unable to find the specified class and method, we can specify an exclusion list to resolve the issue.
[classes] PreControllerClass [methods] preControllerFunction [functions] # This section can be left blank
Once this file has been created, it should be specified in the “Project” > “Project Settings” > “Obfuscation” tab:
Re-encoding the files should now result in the example code working again when obfuscation is enabled.
That’s all there is to resolving issues raised by obfuscation. Most of the issues caused come down to inconsistencies when trying to find variables, classes or functions that have been obfuscated.