I was viewing this question PHP - defining classes inside a function(How it has so many upvotes is beyond me)
In short it does not answer my question.
Specifically speaking following with wordpress plugin creation codex
A shortcode is something you can use in any plugin once created for example
//Create callback for shortcode
function DisplayNumberFive(){
echo "5";
}
//create shortcode as [DNF]
add_shortcode('DNF','DisplayNumberFive');
Now anywhere that I wanted to display the number 5
I can simply do
do_shortcode("[DNF]");
and the output is 5
Now with this functionality in mind would it make sense to have a class for instance
//Create callback for shortcode
function CreateFooClass(){
class Foo{
function Bar(){
return "FooBar";
}
}
}
//Create shortcode as [include_Foo]
add_shortcode('include_foo','CreateFooClass');
//Use case
do_shortcode("[include_foo]");
$foo = new Foo();
echo $foo->Bar();
//Ouput
"FooBar"
My question is if I am doing this wrong or if this introduces bugs that I do not see. Would Bar()
even be accessible since it is within a function?
The goal is to eliminate the need for duplicate files within plugins on wordpress so if you create a class for example for logging events on one plugin but you would also like to log events for another plugin the files in one plugin are not accessible to one another so a duplicate file must be made in the other plugin so to eliminate that creating a shortcode would allow the class to be accessible everywhere within all plugins which is how I came to this solution I am just concerned that It may not be right considering I could not find any google results detailing anyone doing this and it made me question if nobody else has done it there must be a reason why.
So why does nobody do this?