Calling a child in a parent function

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 >>
             // Some code 
          }
   }

   Class Child extends Parent {

          public function B() {
            // Some code
          }
   }

   $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. , :).

+4
3

, oop .

-, db_connect, - -, Model. , - Model, .

-, , , mysqli . , . mysqli - .

, , , . - :

class Car
{
    // why do you need a `$query` member when you are not going to use it?
    // same for `$result`

    private $dbConnection;

    public function __construct(mysqli $dbConnection)
    {
        $this->dbConnection = $dbConnection;
    }

    public function add($marca, $model, $pret, $descriere)
    {
        $query = 'INSERT INTO autoturisme';
        $query.= ' (marca, model, pret, descriere)';
        $query.= ' VALUES';
        $query.= ' (?, ?, ?, ?)';

        $stmt = $this->dbConnection->prepare($query);

        $stmt->bind_param('ssss', $marca, $model, $pret, $descriere);

        if (!$stmt->execute()) {
            throw new \Exception('We are sorry but an error occurred');
        }
    }
}

$mysqli = new mysqli('localhost', 'user', 'pass', 'dbname');

$car = new Car($mysqli);

try {
    $car->add('BMW', '325', 'dunnowhatthismeans', 'description?');
} catch(\Exception $e) {
    echo $e->getMessage();
}

, , , SQL-.

+3

. , .

, .

+2

You can try switching it, so the parent class connects to the database and provides methods to the child class to access this connection. Here is a pseudo code that roughly shows how this will work.

class Parent {

   // method to run SQL statement
   runSQL(SQL) {
      // connect to database (or do it in constructor) and run the SQL
   }
}

class Child extend Parent {
          public function B() {
              ...
              parent::runSQL("SELECT...")
          }
}
+2
source

All Articles