DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.67k stars 2.25k forks source link

add_action: &$this vs 'PluginName' #99

Closed nathanielks closed 10 years ago

nathanielks commented 10 years ago

Thoughts/comments? I see we're using 'PluginName' in plugin-name.php. Does WP create a new instance of that class when it's added like that vs &$this?

nathanielks commented 10 years ago

Answered my own question. Providing the plugin name requires whatever function you're adding to be static. If you pass $this as the class, you can use its instance. Still though, I imagine it shouldn't be use ONLY static functions or ONLY instance functions, yeah?

grappler commented 10 years ago

Are you looking at the latest code of class-plugin-name.php? Please could you share the exact location of the code?

nathanielks commented 10 years ago

Lines 63, 64 in plugin-name.php, for example. 

— Sent from Mailbox for iPhone

On Fri, Oct 25, 2013 at 2:08 AM, Ulrich Pogson notifications@github.com wrote:

Are you looking at the latest code of class-plugin-name.php? Please could you share the exact location of the code?

Reply to this email directly or view it on GitHub: https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate/issues/99#issuecomment-27069773

tommcfarlin commented 10 years ago

Note also that $this and &$this fundamentally differ how data is being based around in the program. One is pass-by-reference, one is pass-by-value.

From the PHP Documentation:

By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

The reason that I no longer use &$this in my plugins is because:

Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.

You have to change the way in which the parameters are passed.

nathanielks commented 10 years ago

Gotcha. Thanks for clearing that up =D