Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use a backslah before global functions in PHP even if is not needed?

Tags:

php

There is a recommendation on https://phptherightway.com/pages/The-Basics.html using backslash for global functions. Take a look at this code:

<?php

 namespace myspace;
 
 class MyClass
 {
      public $myvar = 'test';
      public function myfunction()
      {
           $withoutBackslash = json_encode($this->myvar);
           $withBackslash    = \json_encode($this->myvar);
          return [$withBackslash, $withoutBackslash];
      }
  }

How is better with or without backslash? I never used them before should I change my code in either direction (removing or adding)?

like image 948
Mihai Crăiță Avatar asked Mar 03 '23 07:03

Mihai Crăiță


1 Answers

No, since you do not have global functions, only classes, then there is no point in separate them. They are using \fopen because they have phptherightway\fopen

In that case writing just fopen will refer to phptherightway\fopen instead of global fopen

like image 153
Justinas Avatar answered Mar 05 '23 16:03

Justinas