01/10/2018, 16:15
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:xampphtdocsOOP_PHPOOP_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();
?>
Bài liên quan
Superclass: 4 tham số
Subclass: 5 tham số
=> lỗi vì không cài đặt theo đúng signature của abstract.
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” http://www.php.net/manual/en/language.oop5.overloading.php
1 tên hàm chỉ có 1 signature.
Đây là PHP bạn nhé =_=
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()
andfunc_get_arg()
to get the arguments passed, and use them normally.For example:
Ref: https://stackoverflow.com/questions/4697705/php-function-overloading