文件1
class A {
function a(){
//a方法内容
}
function b(){
//调用文件2中B类的c方法
}
}
文件2
class B {
function c(){
$this->a();
}
}
这样一定出错,因为两个类在不同文件,c方法调用不到A类的a方法。文件2是希望用做公共方法调用,请教有没有解决方案?
...全文
779打赏收藏
请教关于方法的调用
文件1 class A { function a(){ //a方法内容 } function b(){ //调用文件2中B类的c方法 } } 文件2 class B { function c(){ $this->a(); } } 这样一定出错,因为两个类在不同文件,c方法调用不到A类的a方法。文件2是希望用做公共方法调用,请教有没有解决方案?
//文件1
<?php
class A {
function __construct(){
}
public function a(){
//a方法内容
return "i'm A_a()<br/>";
}
public function b(){
//调用文件2中B类的c方法
return "i'm B_b()<br/>";
}
}
//文件2
<?php
require_once("tools.php");
class B extends A{
public function c(){
return "i'mB_c + ".$this->a();
}
}
$tools = new B();
echo $tools -> a();
echo $tools -> b();
echo $tools -> c();
//文件1
<?php
class A {
function __construct(){
}
public function a(){
//a方法内容
return "i'm A_a()<br/>";
}
public function b(){
//调用文件2中B类的c方法
return "i'm B_b()<br/>";
}
}
//文件2
<?php
require_once("tools.php");
//文件2
class B extends A{
public function c(){
return "i'mB_c + ".$this->a();
}
}
$tools = new B();
echo $tools -> a();
echo $tools -> b();
echo $tools -> c();