我如何覆盖其他类中的表,并在第一个和三个字段中显示它?
<?php
class A {
public $tab = array();
public function wyswietl() {
$this->tab['id'] = '1';
foreach ($this->tab as $key => $value) {
print $key .' = '. $value;
}
}
}
class B {
public function dodajB() {
$this->tab['desc'] = 'descriptions';
}
}
class C
{
public function dodajC() {
$this->tab['name'] = 'names';
}
}
$ob = new A;
$ob2 = new B;
$ob2->dodajB();
$ob3 = new C;
$ob3->dodajC();
$ob->wyswietl();
?>
答案 0 :(得分:0)
你的问题令人困惑,但我认为你在寻找:
class B extends A {
public function dodajB(){
$this->tab['desc'] = 'descriptions';
}
}
class C extends B {
...
答案 1 :(得分:0)
ob被定义为A类的一个对象。要获得所有三个列,你需要使它成为C类,并从B扩展C,从A扩展。我还建议使用__construct函数来构造你的对象为了你。您还引用了tab的局部变量而不是使用它来引用它的类。
请参阅下面的代码,其中包含以上所有内容:
<?php
class A
{
public $tab = array();
public function __construct()
{
$this->tab['id'] = '1';
}
public function wyswietl()
{
foreach ($this->tab as $key => $value)
{
print $key .' = '. $value . "\n";
}
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
$this->tab['desc'] = 'descriptions';
}
}
class C extends B
{
public function __construct()
{
parent::__construct();
$this->tab['name'] = 'names';
}
}
$ob = new A;
$ob2 = new B;
$ob3 = new C;
echo 'A: ';
$ob->wyswietl();
echo "\n\n";
echo 'B: ';
$ob2->wyswietl();
echo "\n\n";
echo 'C: ';
$ob3->wyswietl();
echo "\n\n";
?>