Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Constructor (object-oriented programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Syntax == * [[Java (programming language)|Java]], [[C++]], [[C Sharp (programming language)|C#]], [[ActionScript]], {{nowrap|[[PHP]] 4}}, and [[MATLAB]] have a naming convention in which constructors have the same name as the class with which they are associated. * In PHP 5, a recommended name for a constructor is <code>__construct</code>. For backwards compatibility, a method with the same name as the class will be called if <code>__construct</code> method can not be found. Since PHP 5.3.3, this works only for non-namespaced classes.<ref name="php5cpnstructor">[http://www.php.net/manual/en/language.oop5.decon.php Constructors and Destructors], from PHP online documentation</ref> * In PHP 7, you should always name the constructor as <code>__construct</code>. Methods with the same name as the class will trigger an E_DEPRECATED level error.<ref name="php5cpnstructor"/> * In [[Perl]], constructors are, by convention, named "new" and have to do a fair amount of object creation. * In [[Moose perl|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 "<code>New</code>". * In [[Python (programming language)|Python]], the constructor is split over two methods, "<code>__new__</code>" and "<code>__init__</code>". The <code>__new__</code> method is responsible for allocating memory for the instance, and receives the class as an argument (conventionally called "<code>cls</code>"). The <code>__init__</code> method (often called "the initialiser") is passed the newly created instance as an argument (conventionally called "<code>self</code>").<ref>[https://docs.python.org/3/reference/datamodel.html#basic-customization Data model], from Python online documentation</ref> * [[Object Pascal]] constructors are signified by the keyword "<code>constructor</code>" and can have user-defined names (but are mostly called "<code>Create</code>"). * In [[Objective-C]], the constructor method is split across two methods, "<code>alloc</code>" and "<code>init</code>" with the <code>alloc</code> method setting aside (allocating) memory for an instance of the class, and the <code>init</code> method handling the bulk of initializing the instance. A call to the method "<code>new</code>" invokes both the <code>alloc</code> and the <code>init</code> methods, for the class instance.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)