Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The advantage of initializing properties outside of the constructor is that someone reading your code will immediatly know its a default value.</p> <p>Inconvenient is you cannot use every kind of data this way -- will not work with object instanciations, for instance, or with heredoc syntax, from what I recall.</p> <p><br> I don't think there is much of a difference when it comes to performance -- anyway, there are probably lots of things that matter a lot more, in your application ;-)</p> <p><br> Still, purely for fun, using the Vulcan Logic Disassembler :</p> <p>With the first example of code (<code>temp-2.php</code>) :</p> <pre><code>&lt;?php class TestClass { private $test_var = 'Default Value'; function __construct() { } } $a = new TestClass(); </code></pre> <p>You get these opcodes :</p> <pre><code>$ 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. </code></pre> <p>While, with the second example of code (<code>temp-3.php</code>) :</p> <pre><code>&lt;?php class TestClass2 { private $test_var; function __construct() { $this-&gt;test_var = 'Default Value'; } } $a = new TestClass2(); </code></pre> <p>You get those opcodes :</p> <pre><code>$ 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. </code></pre> <p>So, I'm guessing there is a bit of a difference... But not that important ^^</p> <p>Up to you to interpret the opcodes -- but the funny thing is there is no trace of '<code>Default Value</code>' in the first dump... interesting, actually ^^ <br>Seems VLD cannot (or just doesn't) dump everything :-(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload