By GokiSoft.com| 20:43 16/03/2022|
Học PHP

[Video] Tìm hiểu tích chất lập trình OOP trong PHP - Khóa học PHP/MySQL


#people.php


<?php
// Class People -> parent class
class People {
	public $fullname;
	public $birthday;
	public $cmtnd;
	public $address;

	public function echo() {
		echo '<br/>Ho ten: '.$this->fullname.', ngay sinh: '.$this->birthday.', CMTND: '.$this->cmtnd.',  dia chi: '.$this->address;
	}
}


#readme.txt


Nội dung kiến thức
- Giới thiệu về lập trình OOP (Tính chất kế thừa) -> Basic
	- override -> Ghi phuong thuc
- Phát triển 1 dự án MVC: BT2299

Bài toán:
Giả sử viết chương trình quản lý công dân trong thành phố Hà Nội
	- People:
		- Thuộc tính:
			- fullname
			- cmtnd
			- birthday
			- address
		- Hạnh động:
			- display: In dong Fullname
			- processForm
			- insert
			- update
			- delete
			...
	- Student
		- Thuộc tính:
			- fullname
			- cmtnd
			- birthday
			- address
			- rollno
		- Hạnh động:
			- display: In dong Fullname
			- processForm
			- insert
			- update
			- delete
			...
			- learning: Hanh dong hoc

Viết đoạn script -> quản lý danh sách sinh viên & công dân -> Hiển thị thông tin


#student.php


<?php
require_once('people.php');

// Class Student -> Child Class
class Student extends People {
	// thuoc tinh moi
	public $rollno;

	// Hanh dong moi
	public function learning() {
		echo '<br/>Sinh vien dang hoc tap ...';
	}

	// Ghi de phuong thuc -> thanh con -> viet lai ham cua thang cha
	// public function echo() {
	// 	echo '<br/>Ho ten: '.$this->fullname.', ngay sinh: '.$this->birthday.', CMTND: '.$this->cmtnd.',  dia chi: '.$this->address.', msv: '.$this->rollno;
	// }

	public function echo() {
		parent::echo();
		echo ', msv: '.$this->rollno;
	}
}


#vidu.php


<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>OOP in PHP</title>
</head>
<body>
<h1 style="text-align: center;">Welcome to learn OOP in PHP</h1>

<?php
require_once('people.php');
require_once('student.php');

$p = new People();
$p->fullname = 'TRAN VAN A';
$p->birthday = '10/10/1999';
$p->cmtnd = '1231232312';
$p->address = 'Ha Noi';
$p->echo();

$s = new Student();
$s->fullname = 'Sinh Vien A';
$s->birthday = '10/10/2000';
$s->cmtnd = '345345345';
$s->address = 'Nam Dinh';
$s->rollno = 'R001';
$s->echo();
$s->learning();

// $stdList = [];
// $stdList[] = new Student(); //Code nhanh -> co the khai bao tuong minh -> thiet lap thuoc tinh
// $stdList[] = new Student();
// $stdList[] = new Student();
// $stdList[] = new Student();
// $stdList[] = new Student();
?>

</body>
</html>


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó