Answer the question
In order to leave comments, you need to log in
How to make class inheritance?
I have class A, I also need a class that does everything the same as class A, but takes data from another table "table2".
Probably, it is better to pass the table name in the constructor and then the second class is not needed, right?
But I would like to solve this by creating a descendant class in which to change only table1 to table2. How to do it?
<?
class A {
public $c;
public function __construct() {
$query = "SELECT * FROM `table1`;";
//здесь код по раскладке данных из таблицы базы в переменные
}
function f1() {
//код
return $r1;
}
}
?>
Answer the question
In order to leave comments, you need to log in
It is done like this:
class B extends A
{
public function __construct()
{
$query = "SELECT * FROM `table2`;";
}
}
Well, you're right - inheritance is not necessary here at all. If you really want to - you do different constructors in both classes - each one will initit a variable with the table name in its own way.
class Student { }
class GraduateStudent extends Student { }
Well, in general, you can and as the author wants. Something like this.
class A {
private $table = 'table1';
public function __construct() {
// some init
}
public function run () {
// select * from $this->table
}
}
class B extends A {
public function __construct() {
parent::__construct();
// some init
$this->table = 'table2';
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question