Reputation: 447
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?
Upvotes: 0
Views: 152
Reputation: 2815
I understand that Object Oriented Programming has been on the top for over a dozen years, but WP uses a completely different paradigm: Structural programming. So try to avoid mixing this.
And now for your case:
If you use autoloader, then files with classes will be automatically included on first class use.
In addition, you use class definitions straight from PHP4. In PHP5 the keywords public
,private
, protected
has been introduced (to adjust the visibility of methods in classes).
It looks, that do_shortcode
is created to return HTML, so next solution can't be used with it. (But try it, I'm not sure.)
function CreateFooClass(){
if(!class_exists('Foo')) {
class Foo{
public function Bar(){
return "FooBar";
}
}
}
return new Foo()
}
$foo = CreateFooClass();
$foo->Bar();
Upvotes: 1
Reputation: 2395
WordPress shortcodes were never designed to be called in PHP context. They are meant to be used in content. Being able to call shortcode in PHP is more of an implementation detail than something you would normally do.
So, yeah, technically you could use this to call shortcode from PHP to use it as a kind of class load mechanism.
Or you could a real class autoload and $foo = new Foo();
would work without any extra code.
So no one is doing it (or anything like it) because autoload is a thing and works fine.
Upvotes: 0