PHP – Passing data between classes
Static class properties are the most efficient way to pass information from one class to another. Typically, one would place a variable in the global scope for other functions and classes to use. However, this is not a good practice as setting GLOBALS equates to a downgrade in performance. Using static properties allows you to store/retrieve data without instantiating a class object.
class IntermediaryData{
public static $global = null;
public function set($data){
return self::$global = $data;
}
public function get(){
return self::$global;
}
}
Use this in your classes to set and retrieve data:
IntermediaryData::get(); IntermediaryData::set($data);
A good use case for this functionality is passing a rolling list of ids to exclude from queries from widget to widget within a dynamic WordPress sidebar.