HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Variable class from library

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fromvariableclasslibrary

Problem

Can I do something better with this code? It's just a snippet of a library I wrote.

getString()]);
     }
     final public static function destruct(){
         unset(static::$vars);
         static::$vars = array();
     }
     final public static function override(String $name, $value){
         static::clear($name);
         static::register($name, $value);
     }
     final public static function get(String $name){
         $name = $name->getString();
         return (isset(static::$vars[$name]) ? static::$vars[$name] : null);
     }
     final public static function register(String $name, $value){
         return (!is_null(static::get($name)) ? false : static::$vars[$name->getString()] = $value);
     }
 }

Solution

First of all, method and variable docblocks are recommended. Provide a description of each method, its parameters and their return values.

I would give your getter function Variable::variables() an active name like getVariables() and rename your Variable::get() method to getVariable(). Consistency is important and you want to avoid confusion with the magic getter __get().

Are you intentionally using the non-magic method Variable::destruct instead of the PHP magic method __destruct()?

This is more of a syntax preference, but I would use array_key_exists() instead of isset() when referring to the property static::$vars since I find the verbage more aligned with what you are testing.

The docblocks are the only important thing. Good work.

Context

StackExchange Code Review Q#26114, answer score: 2

Revisions (0)

No revisions yet.