Is it allowed to use procedural functions inside OOP PHP methods?
Well the first question would be is it even possible. And why don't we try it out:
class A {
public static function randomStaticMethod() {
echo "Function call:" . phpversion();
}
public function randomMethod() {
echo "Function call:" . phpversion();
}
}
$o = new A();
$o->randomMethod();
A::randomStaticMethod();
And you will see it will work as expected, no errors no warnings, just:
Function call: ...
Function call: ...
So it's definitely possible. Now to your question: Is it allowed?
Simple question, which should answer your question:
How would you get the length of a string, when you wouldn't be allowed to use functions like strlen()
in a class?
So yes it's also definitely allowed and used.
Now a few other things, which might be useful to know. If you work with namespaces you have to be careful.
As example:
namespace I_AM_A_NAMESPACE;
function strlen() {
echo "nope";
}
echo strlen("xyz");
echo \strlen("xyz");
Your output will be:
nope
3
So if you work with namespaces you have to know in which namespace you are and which function you want to call. So if you want to call the global function strlen()
and you want to take the save route, always put a \
in front of it, to make sure you call the global strlen()
function.