0

I have a plugin I am working on, everything is working fine but everything is in my index.php file, all logic is in there. I am currently refactoring and created some classes in separate files. I am unable to currently use these classes inside my plugins functions.

In my index.php file I have a function that handles a pricing shortcode. I also have a pricing class, however I am unable to use it,

$price = new Price();
function pricingShortcodeFunction(){
    return $price->showPrice(); //Should return value but not working in the function
}
Sebastian Flores
  • 101
  • 2
  • 12
  • I am autoloading the classes using Composer/PSR-4 if I put that first line instantiating the Price class inside of the shortcode function, it works fine, but when I instantiate the class outside of the function it does not work. – Sebastian Flores May 31 '19 at 05:42
  • 2
    ^ That is because variables are scoped within functions in PHP. You need to call `global $price` in your function. – Joshua T May 31 '19 at 05:43
  • Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – M. Eriksson May 31 '19 at 05:45

0 Answers0