The way you’ve done it above should work. Once a variable is declared in global scope, it should be globally available (of course, your functions still need to declare it as a global: global $x;
). You could also use the $GLOBALS
superglobal variable:
global $x;
$x->var;
is the same as
$GLOBALS['x']->var;
I personally would use a static variable in your function:
function my_data(){
static $data = null;
if(null === $data)
$data = new MY_Class();
return $data;
}
Then anywhere in your code, you could just write $vars = my_data();
and have the one instantiation of your class.
Hope that helped!
Related Posts:
- How to: inspect global variables in WordPress
- get_post() vs global $post or $GLOBAL[‘post’]
- Does an activated plugin automatically mean its methods are available to other WP functions?
- How to deal with global information without creating a Singleton class
- When and Where is `global $post` Set and Available?
- Using global $post v/s $GLOBALS[‘post’]
- Accessing $post global on a custom post type archive page
- get_query_var vs global query variables?
- What is the global $wp object used for?
- Change global values from templates
- Will $current_user no longer be global?
- Is $page a global variable in wordpress?
- How to prevent XSS alter custom global javascript object & methods in WordPress
- What is the proper method of using global $post?
- How can i use a global variable in a .css file generated with php?
- Current page id returns the incorrect value
- Echo get_the_category() outside of loop (global?)
- How does WordPress make its functions globally available?
- Recommendations on accessing current user data in WordPress
- WP user agent returns random variables
- How to get particular data from wp_list_comments outside the loop?
- How to save a viewer specific option
- What is the correct way to obtain access to the WP_oEmbed object?
- $GLOBALS[‘value1’] is not working
- Creating a (global)-mapping
- wp localize script makes variable global, how to solve that?
- using globals from wp_link_pages function
- Best way to access variables in template markup
- Global Handle to Class unavailable in Plugin?
- Is there a way to natively return the full filesystem path when using $wp_styles?
- What is the purpose of the word ‘self’?
- Understanding Python super() with __init__() methods [duplicate]
- What does ‘super’ do in Python? – difference between super().__init__() and explicit superclass __init__()
- What is Inversion of Control?
- What is an example of the Liskov Substitution Principle?
- What __init__ and self do in Python?
- When should you use a class vs a struct in C++?
- C++ error ‘Undefined reference to Class::Function()’ [duplicate]
- Difference between Inheritance and Composition
- What is the difference between private and protected members of C++ classes?
- What’s the difference between a method and a function?
- How do I implement interfaces in python?
- Reading from file in c++ ifstream
- Are static class variables possible in Python?
- What is polymorphism, what is it for, and how is it used?
- java – invalid method declaration; return type required [duplicate]
- java – invalid method declaration; return type required
- Is C++ an Object Oriented language?
- Difference between virtual and abstract methods
- Difference between abstraction and encapsulation?
- What are metaclasses in Python?
- extends class and implements interface in java
- Separating class code into a header and cpp file
- What is a mixin, and why are they useful?
- What is a “driver class”?
- Error at constructor : Expected an identifier?
- What is difference between functional and imperative programming languages?
- How can I create a copy of an object in Python?
- Prefer composition over inheritance?
- What is Serialization?
- Error “C++ requires a type specifier for all declarations whilst defining methods”
- C++ BlackJack Stuck trying to program Ace
- What are the differences between struct and class in C++?
- What is the meaning of single and double underscore before an object name?
- How to sort a list of objects based on an attribute of the objects?
- When should I use ‘self’ over ‘$this’?
- How to implement a binary search tree in Python?
- What is polymorphism in Javascript?
- Already defined in .obj – no double inclusions
- Polymorphism vs Overriding vs Overloading
- fatal error LNK1169: one or more multiply defined symbols found in game programming
- How to call a parent class function from derived class function?
- Python function overloading
- Meaning of @classmethod and @staticmethod for beginner?
- Meaning of @classmethod and @staticmethod for beginner?
- When should I be using classes in Python?
- What is the difference between object-oriented languages and non object-oriented languages?
- An object reference is required to access a non-static member
- When to use ‘raise NotImplementedError’?
- How to create a subclass in C#?
- How would one write object-oriented code in C?
- Why do some classes require main methods and others do not?
- How to get a JavaScript object’s class?
- What is the syntax for accessing PHP object properties?
- What is method hiding in Java? Even the JavaDoc explanation is confusing
- Pass arguments to Constructor in VBA
- Why use getters and setters/accessors?
- JavaScript pattern for multiple constructors
- Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
- Inheritance vs. Aggregation [closed]
- What is the difference between an Instance and an Object?
- How do you implement a class in C?
- What’s wrong with overridable method calls in constructors?
- Does ECMAScript 6 have a convention for abstract classes?
- Fields in interfaces
- When/why to make function private in class?
- jQuery creating objects [duplicate]
- Explaining Python’s ‘__enter__’ and ‘__exit__’
- What does ‘low in coupling and high in cohesion’ mean
- What is a mixin, and why are they useful?