Reputation: 351
I have created a class to put my local labels inside:
class Local {
public static function closed($lang = 'en') {
$data['nl'] = 'Gesloten';
$data['fr'] = 'Fermer';
$data['en'] = 'Closed';
return $data[$lang];
}
}
So I can call them by entering Local::closed('fr');
One question I'm asking myself: isn't this slowing down the performance of my website since each time I call this public static function a new instance of the Local class is created.
Is there a better way to optimize the performance of this class and if there is, how do I do that?
Upvotes: 1
Views: 48
Reputation: 24661
If you're worried about refilling the array each time you call the class, you can try a technique called memoization. This process basically takes a value and computes it once and saves the result of that computation for use later so that it does not have to be computed again.
class Local {
protected static $translations = [];
public static function closed($lang = 'en') {
// This is the shortcut. The value has already been computed.
if(isset(self::$translations[$lang])) {
return self::$translations[$lang];
}
self::$translations['nl'] = 'Gesloten';
self::$translations['fr'] = 'Fermer';
self::$translations['en'] = 'Closed';
return self::$translations[$lang];
}
}
The downside is that by speeding up your processing, you are also increasing the amount of memory the script takes because that array will be held in memory until the script terminates.
At the end of the day, though, this sort of problem IMHO is more suited towards a configuration-type solution rather than a class-based solution.
Upvotes: 2
Reputation: 2736
No it does not mean that you instatiate the class: STATIC methods are accessible without instancing te class http://php.net/manual/en/language.oop5.static.php
Upvotes: 4