PHP - Hàm __construct
Trình xây dựng cho phép bạn khởi tạo các thuộc tính của một đối tượng khi tạo đối tượng.
Nếu bạn tạo chức năng __construct[]
, PHP sẽ tự động gọi chức năng này khi bạn tạo một đối tượng từ một lớp.
Lưu ý rằng hàm xây dựng bắt đầu với hai dấu gạch dưới [__]!
Chúng ta thấy trong ví dụ dưới đây, rằng việc sử dụng hàm tạo sẽ cứu chúng ta khỏi gọi phương thức set_name [] giúp giảm lượng mã:
Thí dụ
echo $apple->get_name[];
?>
Hãy tự mình thử »
Một vi dụ khac:
Thí dụ
echo $apple->get_name[];
echo "
";
echo $apple->get_color[];
?>
Hãy tự mình thử »
Người xây dựng
__construct [hỗn hợp ...$values
= ""]: VOID[mixed ...$values
= ""]: void
PHP cho phép các nhà phát triển khai báo các phương thức xây dựng cho các lớp. Các lớp có phương thức xây dựng gọi phương thức này trên mỗi đối tượng mới được tạo, do đó, nó phù hợp với bất kỳ khởi tạo nào mà đối tượng có thể cần trước khi nó được sử dụng.
Lưu ý: Các hàm tạo cha mẹ không được gọi là ngầm nếu lớp con định nghĩa một hàm tạo. Để chạy một hàm tạo cha mẹ, một cuộc gọi đến cha mẹ :: __ construct [] trong hàm tạo con là bắt buộc. Nếu trẻ không định nghĩa một hàm tạo thì nó có thể được kế thừa từ lớp cha giống như một phương thức lớp bình thường [nếu nó không được tuyên bố là riêng tư].: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct[] within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method [if it was not declared as private].
Ví dụ số 1 người xây dựng trong kế thừa
1
classSubClass extends BaseClass {
2
function __construct[] {
parent::__construct[];
print "In SubClass constructor\n";
}
}
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass[];// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass[];// In BaseClass constructor
$obj = new OtherSubClass[];
?>
classSubClass extends BaseClass {
3
function __construct[] {
parent::__construct[];
print "In SubClass constructor\n";
}
}
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass[];// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass[];// In BaseClass constructor
$obj = new OtherSubClass[];
?>
classSubClass extends BaseClass {
4
function __construct[] {
parent::__construct[];
print "In SubClass constructor\n";
}
}
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass[];// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass[];// In BaseClass constructor
$obj = new OtherSubClass[];
?>
classSubClass extends BaseClass {
5
function __construct[] {
parent::__construct[];
print "In SubClass constructor\n";
}
}
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass[];// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass[];// In BaseClass constructor
$obj = new OtherSubClass[];
?>
class
32 năm trước ¶
lách ¶
classSubClass extends BaseClass {
7
function __construct[] {
parent::__construct[];
print "In SubClass constructor\n";
}
}
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass[];// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass[];// In BaseClass constructor
$obj = new OtherSubClass[];
?>
classSubClass extends BaseClass {
8
function __construct[] {
parent::__construct[];
print "In SubClass constructor\n";
}
}
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass[];// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass[];// In BaseClass constructor
$obj = new OtherSubClass[];
?>
classSubClass extends BaseClass {
9
function __construct[] {
parent::__construct[];
print "In SubClass constructor\n";
}
}
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass[];// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass[];// In BaseClass constructor
$obj = new OtherSubClass[];
?>
0
__construct[int $x, int $y = 0] {
$this->x = $x;
$this->y = $y;
}
}// Pass both parameters.
$p1 = new Point[4, 5];
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point[4];
// With named parameters [as of PHP 8.0]:
$p3 = new Point[y: 5, x: 4];
?>
112 năm trước ¶
lách ¶
__construct[int $x, int $y = 0] {
2
$this->x = $x;
$this->y = $y;
}
}// Pass both parameters.
$p1 = new Point[4, 5];
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point[4];
// With named parameters [as of PHP 8.0]:
$p3 = new Point[y: 5, x: 4];
?>
14 năm trước ¶
lách ¶
__construct[int $x, int $y = 0] {
3
$this->x = $x;
$this->y = $y;
}
}// Pass both parameters.
$p1 = new Point[4, 5];
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point[4];
// With named parameters [as of PHP 8.0]:
$p3 = new Point[y: 5, x: 4];
?>
__construct[int $x, int $y = 0] {
4
$this->x = $x;
$this->y = $y;
}
}// Pass both parameters.
$p1 = new Point[4, 5];
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point[4];
// With named parameters [as of PHP 8.0]:
$p3 = new Point[y: 5, x: 4];
?>
__construct[int $x, int $y = 0] {
5
$this->x = $x;
$this->y = $y;
}
}// Pass both parameters.
$p1 = new Point[4, 5];
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point[4];
// With named parameters [as of PHP 8.0]:
$p3 = new Point[y: 5, x: 4];
?>
__construct[int $x, int $y = 0] {
6
$this->x = $x;
$this->y = $y;
}
}// Pass both parameters.
$p1 = new Point[4, 5];
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point[4];
// With named parameters [as of PHP 8.0]:
$p3 = new Point[y: 5, x: 4];
?>
__construct[int $x, int $y = 0] {
7
$this->x = $x;
$this->y = $y;
}
}// Pass both parameters.
$p1 = new Point[4, 5];
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point[4];
// With named parameters [as of PHP 8.0]:
$p3 = new Point[y: 5, x: 4];
?>
__construct[int $x, int $y = 0] {
8
$this->x = $x;
$this->y = $y;
}
}// Pass both parameters.
$p1 = new Point[4, 5];
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point[4];
// With named parameters [as of PHP 8.0]:
$p3 = new Point[y: 5, x: 4];
?>
public function
2
E_DEPRECATED
0
E_DEPRECATED
1
E_DEPRECATED
2
E_DEPRECATED
3
E_DEPRECATED
4
class
314 năm trước ¶
prieler tại ABM Dot tại ¶
E_DEPRECATED
6
E_DEPRECATED
7
E_DEPRECATED
8
E_DEPRECATED
9
class
315 năm trước ¶
Yousef Ismaeil cliprz [at] gmail [dot] com ¶