By GokiSoft.com| 19:36 30/11/2022|
Học PHP

[Source Code] Tìm hiểu về Class Object lập trình PHP - C2206L

#animal.php

<?php
//parent class -> lop cha
class Animal {
	public $name;
	public $type;
	public $foodType;

	public function processForm() {
		echo "Lay du lieu tu Form gui len";
	}

	public function display() {
		echo '<br/>Name -> '.$this->name;
	}
}

#cat.php

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

//Child class -> lop con
class Cat extends Animal {
	public $color;
	public $footCount = 4;

	public function showSound() {
		echo 'Meo .. meo ..';
	}
}

#hotel.php

<?php
class Hotel {
	public $name;
	public $area;
	public $address;
	public $ownerName;
	public $contractDate;

	/**public function __construct() {
		echo 'Xin chao<br/>';
	}*/

	/**public function __construct($x, $y, $z) {
		echo 'Xin chao<br/>';
	}*/
	public function __construct($name, $area, $address, $ownerName, $contractDate) {
		$this->name = $name;
		$this->area = $area;
		$this->address = $address;
		$this->ownerName = $ownerName;
		$this->contractDate = $contractDate;
	}

	public function processForm() {
		echo "Lay du lieu tu Form gui len";
	}

	public function display() {
		echo 'Name -> '.$this->name;
	}
}

#readme.txt

OOP:
	- Hiểu lập trình OOP là gì?
	- Tính chất trong lập trình OOP:
		- Tính chât đóng gói -> ???
		- Tính chất kế thừa -> ???
		- Tính chất đa hình
		- Tính chất trừu tượng

		- Interface

Xây dựng 1 hệ thống quản lý khách sạn:
	Tách ra trong hệ thống có nhưng thực thể gì cần triển khải
		Thông tin khách sạn -> Hotel
			Thuộc tính gì cần quản lý
				- name
				- area
				- address
				- ownerName
				- contractDate
				...
			Triển khải nhưng function (methods), hàm gì cho Hotel
				- nhập thông tin
				- hiển thị thông tin
				- xác thực dữ liêu
				...
		Phòng
			...
		Thiết bị
			...
		Gói dịch vụ
		Menu đồ uống
		...

#ungdung.php

<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DB', 'C2206L');

function query($sql) {
	//B1. Mo ket noi toi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DB);
	mysqli_set_charset($conn, 'utf8');

	//B2. Them/sua/xoa/lay du lieu tu database -> insert/update/delete/select
	mysqli_query($conn, $sql);

	//B3. Dong ket noi toi CSDL
	mysqli_close($conn);
}

class User {
	public $fullname;
	public $email;
	public $birthday;
	public $address;
	public $password;

	public function processForm() {
		if(!empty($_POST)) {
			$this->fullname = $_POST['fullname'];
			$this->email = $_POST['email'];
			$this->birthday = $_POST['birthday'];
			$this->address = $_POST['address'];
			$this->password = $_POST['password'];
		}
	}

	public function insert() {
		$sql = "insert into students(fullname, email, birthday, address, password) values ('$this->fullname', '$this->email', '$this->birthday', '$this->address', '$this->password')";
		query($sql);
	}
}

if(!empty($_POST)) {
	$u = new User();
	$u->processForm();
	$u->insert();
}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Register - Page</title>
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

	<!-- Popper JS -->
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h2 class="text-center">Register</h2>
			</div>
			<div class="panel-body">
				<form method="post">
					<div class="form-group">
					  <label for="usr">Name:</label>
					  <input required="true" type="text" class="form-control" id="usr" name="fullname" value="">
					</div>
					<div class="form-group">
					  <label for="email">Email:</label>
					  <input required="true" type="email" class="form-control" id="email" name="email" value="">
					</div>
					<div class="form-group">
					  <label for="birthday">Birthday:</label>
					  <input type="date" class="form-control" id="birthday" name="birthday" value="">
					</div>
					<div class="form-group">
					  <label for="address">Address:</label>
					  <input type="text" class="form-control" id="address" name="address" value="">
					</div>
					<div class="form-group">
					  <label for="password">Password:</label>
					  <input required="true" type="password" class="form-control" id="password" name="password" value="">
					</div>
					<!-- <div class="form-group">
					  <label for="confirmPwd">Confirm Pwd:</label>
					  <input type="password" class="form-control" id="confirmPwd" name="confirmPwd" value="">
					</div> -->
					<p>
						<a href="login.php">I have a account</a>
					</p>
					<button type="submit" class="btn btn-success">Register</button>
				</form>
			</div>
		</div>
	</div>
</body>
</html>

#vidu.php

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

/**
$hotel = new Hotel();
$hotel->name = 'ABC';
$hotel->address = 'Ha Noi';
$hotel->area = 200;
$hotel->ownerName = 'TRAN VAN A';
$hotel->contractDate = '2022-01-12';
*/
$hotel = new Hotel('TRAN VAN A', 200, 'Ha Noi', 'TRAN VAN A', '2022-01-12');

$hotel->display();

require_once('animal.php');
require_once('cat.php');


$a = new Animal();
$a->name = 'A';
$a->display();

$c = new Cat();
$c->name = 'Cat';
$c->display();
Tags:

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

5

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