Constructor (object-oriented Programming) - Syntax

Syntax

  • Java, C++, C#, ActionScript, and PHP 4, have a naming convention in which constructors have the same name as the class of which they are associated with.
  • In PHP 5, a recommended name for a constructor is __construct. For backwards compatibility, a method with the same name as the class will be called if __construct method can not be found. Since PHP 5.3.3, this works only for non-namespaced classes.
  • In Perl, constructors are, by convention, named "new" and have to do a fair amount of object creation.
  • In Moose object system for Perl, constructors (named new) are automatically created and are extended by specifying a BUILD method.
  • In Visual Basic .NET, the constructor is called "New".
  • In Python, the constructor is called "__init__" and is always passed its parent class as an argument, the name for which is generally defined as "self".
  • Object Pascal constructors are signified by the keyword "constructor" and can have user-defined names (but are mostly called "Create").
  • In Objective-C, the constructor method is split across two methods, "alloc" and "init" with the alloc method setting aside (allocating) memory for an instance of the class, and the init method handling the bulk of initializing the instance. A call to the method "new" invokes both the alloc and the init methods, for the class instance.

Read more about this topic:  Constructor (object-oriented Programming)