-3

Yesterday I was reading PHP classes and suddenly something got weird to me. And that was about constructors in C# and PHP.

My question is what is constructor? Isn't it a special methods, used when instantiating a class that can does something like a method and take necessary resources for class like Memory and etc? And if it is, for example when we have a parent class named A which we derive another class named B and create an instance of B and call the B constructor and send some arguments to its parent (A) use :base(args.) (in C#) and use parent::__construct(args) (in PHP) , so is it goes and call the parent constructor , we don't want to exactly take resources for the parent but we just call the parent constructor which do it's for us. What happen exactly here?

I guess in this case simply just called it like a function! :\ i don't know i made my self clear or not ... Ask me if there is any Ambiguity, and Thanks ;)

UPDATED Thank by giving me the negative vote by the way ... look at this link : Microsoft Docs as far i read here it's something more than a simple function which hold a block of codes.(but with differences which you said , no return value and etc.) if it is just a function which called when an instance get created so why if we make it private then we can not make any instance of it anymore? why when we wanted to create a class add a () at the end of the class name?and when we want to pass any args to constructor use those () to send ? [Person new_person = new Person();] if you think also this a question which doesn't show any effort , vote negative again :D

Mohammad Amin
  • 39
  • 1
  • 7
  • 3
    I think you're *basically* there; it is just an initialization method with semantics to enforce that - there's no special voodoo that goes on with a constructor. and they are *basically* just methods that you can't usually call independently... – Marc Gravell Jul 04 '17 at 15:49
  • See [C# constructor execution order](https://stackoverflow.com/questions/1882692/c-sharp-constructor-execution-order). There is a good answer from Jon Skeet there. – Olivier Jacot-Descombes Jul 04 '17 at 16:11

2 Answers2

1

A constructor is a block of code similar to a method that's called when an instance of an object is getting created.

A constructor doesn't have a return type. The name of the constructor must be the same as the name of the class.

Kukic Vladimir
  • 1,010
  • 4
  • 15
  • 22
1

A constructor is basically just a function. The difference between a normal function and a constructor is, that a Constructor

  1. Doesn't have a return value (not even void)
  2. Is named like the class itself (or has a predefined special name)
  3. Is classified as a mandatory function that has to be executed before the object is in an usable state
  4. Can not be called like other functions

The constructor will be called automatically when creating a new instance of the object.

Update:

To understand why you can't create a new instance of something with a private constructor (from outside) look at it as a normal function for a moment.
If you want to call new ClassWithPrivateConstructor() from outside of said class it will not work because the you do not have access to the constructor.

You may now think that a private constructor is useless. But that's not the case. There are valid cases. Eg. singleton Objects.

And for the () think of a constructor as a normal function again. You want to call the constructor. Maybe even with arguments. That's why you need the () when creating a new instance of an object.

Mischa
  • 1,303
  • 12
  • 24