I wrote a function to display an object on each wordpress post:
function pod(){
global $post,$pod;
$id = get_the_ID();
$pod = new WP_POD( $id );
return $pod;
}
I thought $pod is global, so, I can use $pod->stuffs when need, but it doesn’t work.
So, in each function I need to use stuffs in the object, I have to add one line:
$pod = pod()
I think repeatedly calling this function might not good for performence. Is there a way to make this global and accessable by other functions?
You want to avoid the
globalkeyword. If you need to pull in data to a function, its a sure sign that your design is broken (and WordPress is broken). Use Dependency Injection and pass in $post and $pod to the function (that will also prevent spooky action at a distance):However, that still doesnt make much sense. You are not using $post within the function, so why pass it in? Instead, you reach out of the function scope again to fetch some sort of id. And you use that to instantiate a WP_POD instance and assign it back to the global scope.
Why not just do
and then call it with
or just delete the function altogether and just do
Yes, that wont assign the $pod instance to the global scope, but I doubt that you really need it there anyways.
As for performance: you should not worry about performance unless you have profiled your application and found that it’s running slow and that particular code is indeed the reason for it being slow.