I am learning Object oriented PHP and I am wondering if it is allowed to use procedural functions inside methods.
For example in WordPress you have a function get_option();
to get a value by name from the options database table. Is it allowed to do something like this:
class ExampleClass {
public static function ExampleMethod( $optionName ) {
if( get_option( $optionName ) ) {
return get_option( $optionName ) + 20;
}
}
}
Well the first question would be is it even possible. And why don’t we try it out:
And you will see it will work as expected, no errors no warnings, just:
So it’s definitely possible. Now to your question: Is it allowed?
Simple question, which should answer your question:
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:
Your output will be:
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 ain front of it, to make sure you call the global
strlen()
function.You could go completely static with the code below. I just used the number 1 as the parameter and I used
Return 1
as example code so something can be returned. I’d personally recommend allocating objects dynamically especially if your system has limited memory, but here’s my code: