Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 372171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:11:06+00:00 2026-05-12T14:11:06+00:00

Lately I’ve been wondering if there’s a difference between initializing the variables that have

  • 0

Lately I’ve been wondering if there’s a difference between initializing the variables that have a default value on the Constructor VS on the class definition.

Which one is better, taking into consideration the optimization:

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

class TestClass2
{
 private $test_var;
 function __construct()
 {
  $this->test_var = 'Default Value';
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:11:06+00:00Added an answer on May 12, 2026 at 2:11 pm

    The advantage of initializing properties outside of the constructor is that someone reading your code will immediatly know its a default value.

    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.

    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 😉

    Still, purely for fun, using the Vulcan Logic Disassembler :

    With the first example of code (temp-2.php) :

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

    You get these opcodes :

    $ 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.
    

    While, with the second example of code (temp-3.php) :

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

    You get those opcodes :

    $ 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.
    

    So, I’m guessing there is a bit of a difference… But not that important ^^

    Up to you to interpret the opcodes — but the funny thing is there is no trace of ‘Default Value‘ in the first dump… interesting, actually ^^

    Seems VLD cannot (or just doesn’t) dump everything 🙁

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 201k
  • Answers 201k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Make sure your user has execute permission on any folder.… May 12, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer I don't have any numbers but I can tell you… May 12, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer You should be able to use the "ellipsize" property of… May 12, 2026 at 8:15 pm

Related Questions

Lately I had to change some code on older systems where not all of
Lately I've be moving source files around in our source tree. For example placing
Lately I've been in the habit of assigning integer values to constants and simply
Lately I've been using XPathDocument and XNavigator to parse an XML file for a
Lately I have seen a lot of blog posts concerning how to build loosely

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.