PHP OOP Constructor - W3Schools
Có thể bạn quan tâm
PHP OOP - Constructor ❮ Previous Next ❯
PHP - The __construct() Function
A constructor allows you to initialize an object's properties upon creation of the object.
If you create a __construct() function, PHP will automatically call this function when you create an object from a class.
Notice that the construct function starts with two underscores (__)!
We see in the example below, that using a constructor saves us from calling the set_name() method which reduces the amount of code:
Example
<?phpclass Fruit { public $name; public $color; function __construct($name) { $this->name = $name; } function get_name() { return $this->name; }}$apple = new Fruit("Apple"); echo $apple->get_name();?> Try it Yourself »Another example:
Example
<?phpclass Fruit { public $name; public $color; function __construct($name, $color) { $this->name = $name; $this->color = $color; } function get_name() { return $this->name; } function get_color() { return $this->color; }}$apple = new Fruit("Apple", "red");echo $apple->get_name();echo "<br>";echo $apple->get_color();?> Try it Yourself » ❮ Previous Next ❯ ★ +1 Sign in to track progressTừ khóa » Hàm Constructor Trong Php
-
Lập Trình Hướng đối Tượng Trong PHP
-
PHP: Hàm Tạo (Constructor) Và Hàm Hủy (Destructor) | V1Study
-
PHP - Hàm __construct Và __destruct - Viblo
-
Bài 04: Hàm Khởi Tạo (constructor) Và Hàm Hủy (destructor)
-
Hàm Khởi Tạo (constructor) Và Hàm Hủy (destructor) Của Class Trong ...
-
Tự Học PHP | Hàm Tạo - CONSTRUCTOR Trong PHP
-
Hàm __Construct Trong Php Là Gì ? Học Về Hàm ... - VNG Group
-
Học Về Hàm __Construct Trong Php Là Gì ...
-
Kế Thừa Constructor Trong Php - Darkedeneurope
-
Constructor Và Destructor Trong Lập Trình Hướng đối Tượng PHP Là Gì
-
Hàm __Construct Trong Php Là Gì ...
-
Hàm Khởi Tạo Và Hàm Hủy Trong Lập Trình Hướng đối Tượng - Freetuts
-
Constructor Và Destructor Trong PHP Là Gì? - Nguyễn Vũ Khúc
-
Học Về Hàm __Construct Trong Php Là Gì ? Lập ... - Cdsp Ninh Thuận