I would like to know if I can call a child in the parent function. Like this:
class Parent {
public function A() {
<< I want to call the object here >>
}
}
Class Child extends Parent {
public function B() {
}
}
$Child = new Child();
$Child -> B();
Two classes are in different files. The My Child class connects to my database with the B () function. In my Parent class, A (), I am trying to insert data received from filling out a form, but I need a database connection and I don’t know how I can name this object. Note. My code works when I have both functions in the same class.
I did not find a solution, so I will try to publish my real code:
class db_connect extends Model
{
private $dbname = "...";
private $dbuser = "...";
private $dbpass = "...";
private $dbhost = "...";
public $dbc;
public function Connect()
{
$this->dbc = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
if($this->dbc === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}}
}
So that was the Child from Above class and Connect () was B ().
Now parent
class Model
{
public $query;
public $result;
public function proccess_data($marca,$model,$pret,$descriere){
<< I am trying to make a connection here from config.php using the function Connect() >>
$this->query = "INSERT INTO autoturisme (autoturism_id, marca, model, pret, descriere) " .
"VALUES (NULL, '$marca', '$model', '$pret', '$descriere')";
$this->result = mysqli_query(<<Also here I would need the connection>>, $this->query)
or die(mysqli_error(<<And here>>));
if($this->result == 1){
echo "<br/>Your data were processed";
} else {
echo "<br/>We are sorry but an error occurred";
}
$this->close_db();
}
mysqli_query mysqli, . $dbc, Connect() : $this- > dbc. mysqli_error. , :).