Is there anything wrong with this practice example?
class Test {
static $_instance = null; // self
protected $_name = null;
/**
* Get instance
* @return [type] [description]
*/
public static function getInstance() {
if(is_null(self::$_instance))
self::$_instance = new self;
return self::$_instance;
}
/**
* Set name
*/
public static function setName($name) {
self::getInstance()->_name = $name;
}
public static function getName() {
return self::getInstance()->_name;
}
}
And then doing this anywhere:
Test::setName('Some name');
Test::getName(); // returns "Some name"
Instead of:
// somewhere early
$test = new Test;
// somewhere else
function someRandomFunction() {
global $test;
$test->setName('Hello there');
}
// somewhere else
function AnotherRandomFunction() {
global $test;
return $test->getName();
}
I wrote a test that looks like this:
class TestTest extends WP_UnitTestCase {
function testInstance() {
$someName = 'Hello ' . time();
Test::setName($someName);
$this->assertEquals(Test::getName(), $someName);
}
}
This being my very first test I write, I am unsure whether my test and approach to this is considered good practice.