I have a pattern to create class and hold in array to return existing instanced class in future use.
abstract class Models extends Factory {
public static function getPlaces($dbConfigName = "my") {
return parent::getClass($dbConfigName, 'Places');
}
public static function getCategories($dbConfigName = "mo") {
return parent::getClass($dbConfigName, 'Categories');
}
}
And another:
abstract class Factory {
protected static $classes = [];
protected static function getClass($dbConfigName, $model) {
$config = Config::getConfiguration($dbConfigName);
$namespace = "App\\Models\\{$config['driver']}\\{$model}";
$key = $dbConfigName.$namespace;
if (!array_key_exists($key, self::$classes)) {
self::$classes[$key] = new $namespace($config);
$test = self::$classes[$key];
}
return self::$classes[$key];
}
}
The class created with the name of Catogires successfull in first call this function:
$categories = Models::getCategories();
and inside of this categories class i want to create another class to use:
$categoryFields = Models::getPlaces();
Bu the above code inside of Categories class create again Categories instead of Places class. In my test with reflection and print the namespace before create class is this:
***** This is for create Categories class for first time*****
New: App\Models\MySQL\Categories
ReflectionClass Object
(
[name] => App\Models\MySQL\Categories
)
***** This is for create Places class inside of categories class *****
New: App\Models\MySQL\Places
ReflectionClass Object
(
[name] => App\Models\MySQL\Categories
)
Aucun commentaire:
Enregistrer un commentaire