2009-09-27 2 views
11

Ultimamente mi sono chiesto se c'è una differenza tra l'inizializzazione delle variabili che hanno un valore predefinito su Constructor VS nella definizione della classe.Impostazione delle variabili su Constructor VS sulla definizione della classe

Qual è la migliore, tenendo in considerazione l'ottimizzazione:

class TestClass 
{ 
private $test_var = 'Default Value'; 
function __construct() 
{ 
} 
} 

class TestClass2 
{ 
private $test_var; 
function __construct() 
{ 
    $this->test_var = 'Default Value'; 
} 
} 
+0

Hanno già default valore ... non c'è bisogno di iniziarli di nuovo. –

risposta

25

Il vantaggio di inizializzare le proprietà al di fuori della funzione di costruzione è che qualcuno che legge il codice lo riconoscerà immediatamente come valore predefinito.

Scomodo è che non è possibile utilizzare tutti i tipi di dati in questo modo - non funzionerà con le istanze di oggetti, ad esempio, o con la sintassi di heredoc, da ciò che ricordo.


Non credo ci sia molta differenza quando si tratta di prestazioni - in ogni caso, ci sono probabilmente un sacco di cose che contano molto di più, nella vostra applicazione ;-)


Eppure, per puro divertimento, utilizzando il Vulcan Logic Disassembler:

Con il primo esempio di codice (temp-2.php):

<?php 
class TestClass { 
    private $test_var = 'Default Value'; 
    function __construct() { 
    } 
} 
$a = new TestClass(); 

È possibile ottenere questi codici operativi:

$ php -d extension=vld.so -d vld.active=1 temp-2.php 
Branch analysis from position: 0 
Return found 
filename:  /home/squale/developpement/tests/temp/temp-2.php 
function name: (null) 
number of ops: 11 
compiled vars: !0 = $a 
line  # op       fetch   ext return operands 
------------------------------------------------------------------------------- 
    2  0 EXT_STMT 
     1 NOP 
    7  2 EXT_STMT 
     3 ZEND_FETCH_CLASS         :1  'TestClass' 
     4 EXT_FCALL_BEGIN 
     5 NEW            $2  :1 
     6 DO_FCALL_BY_NAME        0 
     7 EXT_FCALL_END 
     8 ASSIGN             !0, $2 
     9 RETURN             1 
     10* ZEND_HANDLE_EXCEPTION 

Class TestClass: 
Function __construct: 
Branch analysis from position: 0 
Return found 
filename:  /home/squale/developpement/tests/temp/temp-2.php 
function name: __construct 
number of ops: 4 
compiled vars: none 
line  # op       fetch   ext return operands 
------------------------------------------------------------------------------- 
    4  0 EXT_NOP 
    5  1 EXT_STMT 
     2 RETURN             null 
     3* ZEND_HANDLE_EXCEPTION 

End of function __construct. 

End of class TestClass. 

Mentre, con il secondo esempio di codice (temp-3.php):

<?php 
class TestClass2 { 
    private $test_var; 
    function __construct() { 
     $this->test_var = 'Default Value'; 
    } 
} 
$a = new TestClass2(); 

si ottiene quei codici operativi:

$ php -d extension=vld.so -d vld.active=1 temp-3.php 
Branch analysis from position: 0 
Return found 
filename:  /home/squale/developpement/tests/temp/temp-3.php 
function name: (null) 
number of ops: 11 
compiled vars: !0 = $a 
line  # op       fetch   ext return operands 
------------------------------------------------------------------------------- 
    2  0 EXT_STMT 
     1 NOP 
    8  2 EXT_STMT 
     3 ZEND_FETCH_CLASS         :1  'TestClass2' 
     4 EXT_FCALL_BEGIN 
     5 NEW            $2  :1 
     6 DO_FCALL_BY_NAME        0 
     7 EXT_FCALL_END 
     8 ASSIGN             !0, $2 
    9  9 RETURN             1 
     10* ZEND_HANDLE_EXCEPTION 

Class TestClass2: 
Function __construct: 
Branch analysis from position: 0 
Return found 
filename:  /home/squale/developpement/tests/temp/temp-3.php 
function name: __construct 
number of ops: 7 
compiled vars: none 
line  # op       fetch   ext return operands 
------------------------------------------------------------------------------- 
    4  0 EXT_NOP 
    5  1 EXT_STMT 
     2 ZEND_ASSIGN_OBJ           'test_var' 
     3 ZEND_OP_DATA            'Default+Value' 
    6  4 EXT_STMT 
     5 RETURN             null 
     6* ZEND_HANDLE_EXCEPTION 

End of function __construct. 

End of class TestClass2. 

Quindi, sto cercando di indovinare lì è un po 'diverso ... Ma non così importante ^^

Fino a voi per interpretare i codici di accesso - - ma la cosa divertente è che non c'è traccia di 'Default Value' nel primo dump ... interessante, in realtà ^^
Sembra VLD non può (o semplicemente non) scaricare tutto :-(

+3

+1 per php smontare – elcuco

+0

Risposta eccellente :) Grazie – MarioRicalde

+0

Prego :-) Buon divertimento! –

3

penso si riduce per lo più verso il basso per preferenze personali. Tuttavia, ci sono alcuni valori che non è possibile impostare direttamente sulla variabile, come le nuove istanze di classe, che è necessario assegnare nel costruttore.