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

  • SEARCH
  • Home
  • 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 9136511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:57:44+00:00 2026-06-17T08:57:44+00:00

For example is there a difference between the two? Is one preferred to the

  • 0

For example is there a difference between the two? Is one preferred to the other?

Class Node{    
    public $parent = null;
    public $right = null;
    public $left = null;            
    function __construct($data){
        $this->data = $data;                    
    }
}

Class Node{     
    function __construct($data){
        $this->data = $data;      
        $this->parent = null;       
        $this->left = null;       
        $this->right = null;               
    }
}
  • 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-06-17T08:57:45+00:00Added an answer on June 17, 2026 at 8:57 am

    There are certain differences, yes:

    #1: The class is not formally considered to have these properties if you only define them in the constructor

    Example:

    class Foo {
        public $prop = null;
    }
    
    class Bar {
        public function __construct() {
            $this->prop = null;
        }
    }
    
    var_dump(property_exists('Foo', 'prop')); // true
    var_dump(property_exists('Bar', 'prop')); // false
    
    $foo = new Foo;
    $bar = new Bar;
    
    var_dump(property_exists($foo, 'prop')); // true
    var_dump(property_exists($bar, 'prop')); // true
    

    In addition to the different runtime behavior, it is bad form to use the constructor to “add” properties to your class. If you intend all objects of this class to have the property (which should be practically all of the time) then you should also formally declare them. The fact that PHP allows you to get away with this does not excuse the haphazard class design.

    #2: You cannot initialize properties to non-constant values from outside the constructor

    Example:

    class Foo {
        public $prop = 'concatenated'.'strings'; // does not compile
    }
    

    More examples regarding this constraint are provided in the PHP manual.

    #3: For values initialized inside the constructor, if a derived class omits calling the parent constructor the result might be unexpected

    Example:

    class Base {
        public $alwaysSet = 1;
        public $notAlwaysSet;
    
        public function __construct() {
            $this->notAlwaysSet = 1;
        }
    }
    
    class Derived extends Base {
        public function __construct() {
            // do not call parent::__construct()
        }
    }
    
    $d = new Derived;
    var_dump($d->alwaysSet); // 1
    var_dump($d->notAlwaysSet); // NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference between the following two examples and should one be preferred
Is there any difference between the below two snippets? One is a char array,
For sessions and cookies, is there a difference between example.com and www.example.com? I have
Is there any difference between these two code snippets in terms of memory usage
What is the semantic difference between these two snippets of code? Example 1 var
Is there any difference between the following: (Is there any reason to avoid example
Is there any major difference between the two? Is there anything that can be
I'm trying to find out the difference between using one colon : and two
Is there a difference between the two examples below for beginning a Perl script?
There are any differences between using SetValue with (for example) Canvas.LeftProperty and Margin Property?

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.