What does a class constructor do?
I'm dumbfounded. I've been looking for a nice definition as to what a class constructor(specifically in PHP) exactly does.
As I understand it, it's the initial function that the class executes, wh开发者_运维百科en called, correct?
It is called whether or not we specifically call the function itself?
Can anyone shed some light here? Or at least point me in the right direction.
PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
A constructor creates an Object of the class that it is in by initializing all the instance variables and creating a place in memory to hold the Object.
For example you may want to initialize an integer variable to 10 or 20 based on a certain condition, when your class is created. In such a case you cannot hard code the value during variable declaration. such kind of code can be placed inside the constructor so that the initialization would happen when the class is instantiated.
When a PHP class is first called, the class will automatically run the class constructor function, which can help automatically configure the class. This can be useful if you need to preset some instance variables, sessions or cookies – prior to using the class methods.
A good read on Constructors in PHP
You're more or less correct - it is a function that is called when the class is instantiated (created with the new
keyword).
You shouldn't have to specifically call it unless you extend a class and override the constructor, because a subclasses __construct()
doesn't call it's parent constructor unless explicitly defined with parent::__construct()
.
E.g.
class A {
public $value;
function __construct() {
$this->value = "A";
}
}
$a = new A();
echo $a->value; // "A";
class B extends A {
}
$b = new B();
echo $b->value; // "A";
class C extends A {
function __construct() {
$this->value = "C";
}
}
$c = new C();
echo $c->value; // "C";
class D extends A {
function __construct() {
$this->value = "D";
parent::__construct();
}
}
$d = new D();
echo $d->value; // "D";
A constructor is called whenever a new instance (i.e. object) of a class is created. This allows you to setup the state of the object, as well as pass in any dependencies that the object might need to work properly, such as a CacheManager, or a database connection, or...
If you have an inheritance chain, i.e. Class C extends Class B which extends Class A, then remember to explicitly call the parent constructor if you override the constructor somewhere further down the chain:
<?php
class C extends B
{
public function __construct ($constructorParams)
{
parent::__construct($constructorParams);
}
}
The constructor should NOT return a value.
As I understand it, it's the initial function that the class executes, when called, correct?
It's the method called when a new instance of the class is first created. It's an instance method, because it only works on instances. (An object is an instance; a class is something to which an object belongs. It's possible to have methods belong to the class, not to an instance. PHP calls these static methods, but that isn't always the terminology other languages use.)
In some languages, the constructor actually returns the new object. This is not the case in PHP, where the constructor exists for initialization purposes.
It is called whether or not we specifically call the function itself?
Correct. It's called when you use the new
operator.
class Foo {
public function __construct() { echo "I foo'd.\n" ;}
}
$f = new Foo(); // Echos "I foo'd.\n"
You probably want to read over the PHP manual sections about classes and objects.
No. No. Yes.
Constructor is only called when you create a object. It is not called when you later only use your object somewhere.
When you create a new object, a constructor is a method that is called and it initialize the variables/fields of an object.
A constructor must have the same name as the class its in.
Yes. From Wikipedia -
In PHP (version 5 and above), the constructor is a method named __construct(), which the keyword new automatically calls after creating the object. It is usually used to automatically perform various initializations such as property initializations. Constructors can also accept arguments, in which case, when the new statement is written,you also need to send the constructor the function parameters in between the parentheses.
- Wikipedia PHP Constructor Definition
- PHP Site: What Constructors do ?
It's called when you create a new instance of the class, which is called automatically and you can initiate any local variables there. To do works which has to be done before freeing / destroying your object you can use the Destructor. Constructors and Destructors are'nt PHP specific but general for OO Programming languages. See this
#include<iostream>
using namespace std;
class shape{
protected:
int w, h, a;
public:
shape(int, int);
};
shape :: shape(int x, int y){
w = x;
h = y;
};
class triangle : public shape{
public:
void disp_area();
};
void triangle :: disp_area(){
a = (w + h) * 1 / 2;
cout<<endl<<" Area Of Triangle : "<<a;
};
class rectangle : public shape{
public:
void disp_area();
};
void rectangle :: disp_area(){
a = w * h;
cout<<endl<<" Area Of Rectangle : "<<a;
};
int main(){
triangle obj1;
rectangle obj2;
obj1.shape(2, 3);
obj2.shape(4, 5);
obj1.disp_area();
obj2.disp_area();
return 0;
};
精彩评论