Nạp chồng trong phương thức abstract trong php

Mọi người cho mình hỏi là : Mình có 1 abtract class là lớp cha và khai báo các phương thức abstract trong class đó . Sau đó mình dùng 1 class khác kế thừa class abstract cha. Sau đó mình nạp chồng phương thức trong class con đó liệu có đúng không ? **Tại sao khi mình chạy chương trình nó toàn báo lỗi :

Fatal error: Declaration of SachBaiTap::Khoitao($ts, $nxb, $gb, $slph, $stp) must be compatible with Sach::Khoitao($ts, $nxb, $gb, $slph) in C:\xampp\htdocs\OOP_PHP\OOP_E73508.php on line 68**

Ví dụ

<?php
abstract class Sach{
public $tensach;
public $namxuatban;
public $giabia;
public $soluongphathanh;
abstract public function Khoitao($ts,$nxb,$gb,$slph);
 abstract public function Tinhnhuanbut();
abstract public function Inthongtin();
}
class SachGiaoKhoa extends Sach{
	public function Khoitao($ts,$nxb,$gb,$slph){
	$this->tensach = $ts;
	$this->namxuatban = $nxb;
	$this->giabia = $gb;
	$this->soluongphathanh = $slph;
	}
	public function Tinhnhuanbut(){
		$tiennhuanbut = 0;
		if($this->soluongphathanh > 2000){
		$tiennhuanbut = 0.06 * $this->giabia * $this->soluongphathanh;	
			}
		else{
		$tiennhuanbut = 0.08 * $this->giabia * $this->soluongphathanh;	

			}
			return $tiennhuanbut;
		}
	public function Inthongtin(){
	//echo Tinhnhuanbut("1500,500");
	echo $this->Tinhnhuanbut();
		}
	}
	
class SachBaiTap extends Sach{
	public $soTap;
	public function Khoitao($ts,$nxb,$gb,$slph,$stp){
	$this->tensach = $ts;
	$this->namxuatban = $nxb;
	$this->giabia = $gb;
	$this->soluongphathanh = $slph;
	$this->soTap = $stp;
	}
	public function Tinhnhuanbut(){
	$tiennhuanbut = 0;
	if($this->soTap == 1){
	$tiennhuanbut = 0.05 * $this->giabia * $this->soluongphathanh;	
			}
	else if($this->soTap == 2){
	$tiennhuanbut = 0.06 * $this->giabia * $this->soluongphathanh;	
		}
	else {
	$tiennhuanbut = 0.08 * $this->giabia * $this->soluongphathanh;
		}
		return $tiennhuanbut;
	}
	public function Inthongtin(){
		echo $this->Tinhnhuanbut();
		}
	}
class SachThamKhao extends Sach{
	public $soTrang;
	public function Khoitao($ts,$nxb,$gb,$slph,$st){
	$this->tensach = $ts;
	$this->namxuatban = $nxb;
	$this->giabia = $gb;
	$this->soluongphathanh = $slph;
	$this->soTrang = $st;
	
	}
	public function Tinhnhuanbut(){
		$tiennhuanbut = 0;
	if($this->soTrang >=500 && $this->soluongphathanh > 2000){
		$tiennhuanbut = 0.08 * $this->giabia * $this->soluongphathanh;
	}
	else{
	$tiennhuanbut = 0.06 * $this->giabia * $this->soluongphathanh;
		}
		return $tiennhuanbut;
	}

	public function Inthongtin(){
		echo $this->Tinhnhuanbut();
		}
	}
$Toan = new SachGiaoKhoa();
$Toan->Khoitao("Toan","KimDong",500,1200);
$Toan->Inthongtin();
$BT_Toan = new SachBaiTap();
$BT_Toan->Inthongtin();
$GiaiBT = new SachThamKhao();
$GiaiBT->Inthongtin();
?>

Superclass: 4 tham số
Subclass: 5 tham số
=> lỗi vì không cài đặt theo đúng signature của abstract.

1 Like

mình nhớ hình như trước mình học java mình nạp chồng phương thức vẫn được mà !

PHP không cho dùng overloading mà gọi cái này là “overload” :expressionless: http://www.php.net/manual/en/language.oop5.overloading.php
1 tên hàm chỉ có 1 signature.

3 Likes

Đây là PHP bạn nhé =_=

1 Like

Cho mình hỏi thế giờ khắc phục làm sao ??

You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name. Class method overloading is different in PHP than in many other languages. PHP uses the same word but it describes a different pattern.

You can, however, declare a variadic function that takes in a variable number of arguments. You would use func_num_args() and func_get_arg() to get the arguments passed, and use them normally.

For example:

function myFunc() {
    for ($i = 0; $i < func_num_args(); $i++) {
        printf("Argument %d: %s\n", $i, func_get_arg($i));
    }
}

/*
Argument 0: a
Argument 1: 2
Argument 2: 3.5
*/
myFunc('a', 2, 3.5);

Ref: https://stackoverflow.com/questions/4697705/php-function-overloading

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?