Reputation: 3866
Let's say I have the Debug helper with a method to display a variable's content in a way similar to this:
namespace app\components;
class D extends \yii\base\Component
{
public static function trace($variable='')
{
echo $variable;
}
}
Is there a way for this component to be available in any Controller
, Model
and View
by using its simple form and simply write:
D::trace($bob);
I would like to know if it's possible to import it everywhere so I wouldn't have to use one of these
// Load in config then use this (too long)
Yii::$app->D->trace($key);
// Write the whole namespace everytime (too long)
\app\components\D::trace($value);
// Load the namespace first every time I need it first (Annoying)
use app\components\D;
D::trace($value);
Upvotes: 0
Views: 42
Reputation: 22174
You have two choices:
Create helper in global namespace and add leading slash everywhere:
\D::trace($value);
Create global function as a wrapper for helper (or its methods):
function d() {
static $d;
if ($d === null) {
$d = new D();
}
return $d;
}
d()->trace($string)
or
function dtrace($string) {
return D::trace($string);
}
dtrace($string);
Upvotes: 1