TooBasic: MagicProp
MagicProp?
If you've been reading other TooBasic document pages, you may came across
something like this $this->model->user where the question is what is
$this->model?.
Well, that is a magic property that allows you to access your site's models in an
easy way.
How does it work?
Internally, it gives you access to core factories like ModelsFactory or ItemsFactoryStack, or core singletons like Params or Paths. If you prefer, you may access this core classes directly, but the MagicProp method may be easier.
Where can I use it?
At this point, you may use these magic properties inside any of these:
- Controllers
- Layouts
- Services
- Models
- Shell Tools
- Shell Crons
- Emails
Known properties
The properties you may use are:
$this->model: To access any model defined of your site (visit Models).$this->representation: To access any representation factory of your site (visit Representations).$this->params: To access the singletonParamsand then any of its public methods. For example:return isset($this->params->get->userid);$this->paths: To access the singletonPathsand then any of its public methods. For example:$namesConfigPath = $this->paths->configPath("known_names", \TooBasic\Paths::EXTENSION_JSON);$this->translate: To access the singletonTranslateand then any of its public methods. For example:echo $this->translate->key_hello_world;$this->cache: To access current cache adapter (visit Cache).$this->config: To access the singletonConfigsManagerand therefore JSON configuration files (visit Cache).
My own
Now suppose you are inside one of your own classes and you don't have these magic properties. Well, don't panic, there's is a way to obtain this behavior using a singleton called MagicProp, something like this:
<?php
class MyClass {
protected $_magic = false;
public function __construct() {
$this->_magic = \TooBasic\MagicProp::Instance();
}
public function sayHello() {
echo $this->_magic->translate->key_hello_world;
}
}
Or even better:
<?php
class MyClass {
protected $_magic = false;
public function __construct() {
$this->_magic = \TooBasic\MagicProp::Instance();
}
public function __get($prop) {
$out = false;
try {
$out = $this->_magic->{$prop};
} catch(\TooBasic\MagicPropException $ex) {}
return $out;
}
public function sayHello() {
echo $this->translate->key_hello_world;
}
}
MagicPropException
If you look closely, in the second example we're using a try-catch sentence and
trapping an exception called \TooBasic\MagicPropException which is raised when
an unknown property is requested.
Dynamic Properties
As far as we've told, there are just a few possible parameters that point to a
specific set of singletons, but that's no completely true.
If for example you create a singleton called MySingleton and you want to access
it through magic props using the name ms or even mys, you can add this piece
of code to, for example, your configuration file:
$MagicProps[GC_MAGICPROP_PROPERTIES]['ms'] = 'MySingleton';
$MagicProps[GC_MAGICPROP_ALIASES]['mys'] = 'ms';
In this example you can see an association between the name ms and your
singleton which means you can do something like this, for example, inside a model:
. . .
public function myMethod() {
return $this->ms->someMethod();
}
. . .
Also, in the previous example you can see the association between the name mys
and ms which serves as an alias and you can also do this:
. . .
public function myMethod() {
return $this->mys->someMethod();
}
. . .
Suggestions
Here you have a few links you may want to visit: