Jump to content

Calling/Manipulating API Variables inside Module: Does it matter in which way?


Orkun
 Share

Recommended Posts

Hi Guys

I have an helper module where I am doing different Hooks and other additional logic for a website I am developing. I looked into the code of the module and noticed something. I am using API Variables in different ways. For example:

 

// Creating FilenameArrays

wire("config")->siteScripts = new FilenameArray();

 

// Creating logs

wire("log")->save("selector", $event->return);

 

// Getting languages

$this->wire("languages") 

 

// Setting Debug mode (Depending on roles/usernames

 $this->config->debug = true;

etc...

Now my question is, does it matter if I am using...

wire("apivar")

$this->wire("apivar")

$this->apivar

 

And does it matter if I am mixing these method of accessing the api variables?

PS: Module is extending WireData

Link to comment
Share on other sites

/**
 * Global function, therefore does work everywhere, 
 * but will always refer to the first pw instance
 * in case of multi instance usage.
 * Because of that it's discouraged to use this in
 * modules.
 */
wire("apivar")

/**
 * Safest way to get the api variable in wire derived
 * classes like modules.
 */
$this->wire("apivar")

/**
 * Does work as well, but some (core) classes have that
 * type of access disabled. It also prevents you from 
 * using a custom paramater $apivar in your class.
 */
$this->apivar

 

  • Like 5
Link to comment
Share on other sites

36 minutes ago, LostKobrakai said:

/**
 * Global function, therefore does work everywhere, 
 * but will always refer to the first pw instance
 * in case of multi instance usage.
 * Because of that it's discouraged to use this in
 * modules.
 */
wire("apivar")

/**
 * Savest way to get the api variable in wire derived
 * classes like modules.
 */
$this->wire("apivar")

/**
 * Does work as well, but some (core) classes have that
 * type of access disabled. It also prevents you from 
 * using a custom paramater $apivar in your class.
 */
$this->apivar

 

Thanks for the clarification @LostKobrakai. I think from now on I will always use

$this->wire('apivar')

because of consistency.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...