Reputation: 548
I couldn't find the answer for this one in a while. But for my current project I would really love to have a class instance, which would be globally accessible from any website script, pretty much like $woocommerce
variable in WordPress. Thanks.
Upvotes: 0
Views: 144
Reputation: 1571
I think for this case you should apply Singleton pattern. It should be like below script. For more information about Singleton, please ask Google.
<?php
class YourClass {
private static $instance;
private __construct() {
}
/**
*@return YourClass
*/
public static getInstance() {
if (empty(self::$instance)) {
self::$instance = new self();
**//maybe your $woocommerce here.**
}
return self::$instance;
}
}
//And any PHP files
$singleton = YourClass::getInstance();
In most case, I prefer using Singleton to global variable, it let you easy maintain your code and well support from IDE.
Upvotes: 1