Array and override (no global array)
How i can override the table in other classes, and display it in the first And with three fields?
<?php
clas开发者_高级运维s 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();
?>
ob is defined as an object of class A. To get all three columns you would need to make it of class C and have C extend from B which extends from A. I would also recommend using the __construct function to construct your objects for you. Also you were referencing a local variable of tab rather than the class one using this to reference it.
See the code below which does all of the above:
<?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";
?>
You're question is confusing, but I think you are looking for:
class B extends A {
public function dodajB(){
$this->tab['desc'] = 'descriptions';
}
}
class C extends B {
...
精彩评论