0

what's the difference from using a direct call a class or use it on the top?

Case 1:

use Carbon\Carbon;

...

public function __construct() {

    echo Carbon::now();
}

Case 2

public function __construct() {

    echo \Carbon\Carbon::now();
}

which is better? maybe from the cost / load time or anything else? I know the case 1 is much cleaner, but I think there are more pros and cons from both of them.

Maybe someone already asking about this, but sorry I don't know what's the keywords, so I'm going to ask it here. Thanks !

  • I would say there is zero difference except for flexability if you change the `use` delcaration in the future. There will be no performance differences. – Scuzzy Dec 14 '18 at 05:02
  • @Scuzzy I see, so it's the same performance, the only thing that difference is more tidier or not – Khrisna Gunanasurya Dec 14 '18 at 05:54

2 Answers2

1

There will be difference in final opcode. First case will have one more opcode instruction than second case. It means that first case will take more resources during execution than second.

But frankly case 1 looks cleaner and easier to read.

Bellow opcodes for these cases.

Case 1

function name: (null)
L1-12 {main}() /tmp/test/2.php - 0x7fface082140 + 5 ops
 L2    #0     NOP                                                                                   
 L3    #1     NOP                                                                                   
 L5    #2     EXT_STMT                                                                              
 L5    #3     NOP                                                                                   
 L12   #4     RETURN<-1>              1                                                            

user class: another\App
1 methods: __construct

function name: __construct
L7-10 another\App::__construct() /tmp/test/2.php - 0x7fface0668c0 + 9 ops
 L7    #0     EXT_NOP                                                                               
 L9    #1     EXT_STMT                                                                              
 L9    #2     INIT_STATIC_METHOD_CALL "Carbon\\Carbon"     "now"                                    
 L9    #3     EXT_FCALL_BEGIN                                                                       
 L9    #4     DO_FCALL                                                          @0                  
 L9    #5     EXT_FCALL_END                                                                         
 L9    #6     ECHO                    @0                                                            
 L10   #7     EXT_STMT                                                                              
 L10   #8     RETURN<-1>              null 

Case 2

function name: (null)
L1-11 {main}() /tmp/test/1.php - 0x7f2e3c493000 + 4 ops
 L2    #0     NOP                                                                                   
 L4    #1     EXT_STMT                                                                              
 L4    #2     NOP                                                                                   
 L11   #3     RETURN<-1>              1                                                             


user class: another\App
1 methods: __construct

function name: __construct
L6-9 another\App::__construct() /tmp/test/1.php - 0x7f2e3c466780 + 9 ops
 L6    #0     EXT_NOP                                                                               
 L8    #1     EXT_STMT                                                                              
 L8    #2     INIT_STATIC_METHOD_CALL "Carbon\\Carbon"     "now"                                    
 L8    #3     EXT_FCALL_BEGIN                                                                       
 L8    #4     DO_FCALL                                                          @0                  
 L8    #5     EXT_FCALL_END                                                                         
 L8    #6     ECHO                    @0                                                            
 L9    #7     EXT_STMT                                                                              
 L9    #8     RETURN<-1>              null 
Ogara
  • 31
  • 4
-1

Here's my take.

use Carbon\Carbon;

...

public function __construct() {

    echo Carbon::now();
}

In above example, the constructor is tightly coupled with class Carbon\Carbon. In this case, it would be difficult to extend the class.

Similarly, in case 2 as well the class Carbon\Carbon is tightly coupled with the constructor.

However, there is a third case. In this case, you'd inject the class Carbon\Carbon into the constructor which is called dependency injection. This is more authentic and recommended way than the previous two as in this case the class is not tightly coupled with class Carbon\Carbon anymore. Here's how the above example would look like in this case,

public function __construct(\Carbon\Carbon $carbon) {

    echo $carbon::now();
}
Amit Merchant
  • 1,045
  • 6
  • 21