I’m coding a WordPress plugin and I’m not sure regarding the function name conflict..
I have a file named test_handling.php
which contains the following content :
function testing() { echo 'test'; }
I included this file in a class constructor (file named testcls.class.php
) :
class TestCls {
function __construct() {
require_once('test_handling.php');
testing();
}
function otherfunction() {
testing();
}
// ...
}
In this case, I would like to know if the testing()
function is only available in the TestCls
class, or can it create conflicts if an other WP plugin has a function with the same name ?
Even with the same name, the functions will have different scope if defined as class method. To make a call to a regular function you will do the following:
and the result will be:
the class method need an instance of the class or be statically called. To call the method class you will need the following formats:
or
To sum up, the function test will be different if defined as class method. Then, you will not have conflicts.
Other way to encapsulate your function and make sure you are using the right one is with namespaces. If you use a namespace in your test_handling.php
You will access the function test like this:
Now you are sure about the function you are calling.
from
include
in PHP manualWhich means that yes, you can have conflicts.