I have seen and used Yii::$app->session… and \Yii::$app->session… in my code, both are working.
I don’t know where to use Yii::$app… and where to use \Yii::$app…?
Will there be any difference in performance?
I have seen and used Yii::$app->session… and \Yii::$app->session… in my code, both are working.
I don’t know where to use Yii::$app… and where to use \Yii::$app…?
Will there be any difference in performance?
If you define a namespace and don’t add a use Yii statement, you must use \Yii to indicate that this object is not inside your namespace.
I don’t know if this helps, but I never add this slash by default. If execution fails because a class is not found in current namespace I add a use for absent namespace o the slash to a concrete object.
Thanks for clarification, I used this slash randomly at many place and was wondering what its real purpose was.
I recommend you to read official namespace documentation:
<?php
namespace A\B\C;
class Exception extends \Exception {}
$a = new Exception('hi'); // $a is an object of class A\B\C\Exception
$b = new \Exception('hi'); // $b is an object of class Exception
$c = new ArrayObject; // fatal error, class A\B\C\ArrayObject not found
?>
Thank you very much. As per my understanding of that documentation, if we declare
use Yii;
then we can use Yii::$app->…
and if we do not declare use Yii;
we use \Yii::$app->…
This is correct if you are inside a namespace. If you are not, you can use Yii without slash