Instantiating a class in another constructor in PHP?

Asked

Viewed 373 times

-1

I have a class called teacher who is heir to another so-called person, I am trying to instantiate a third class of connection with bank in the method constructing the Teacher class, but it is returned to me

Fatal error: Uncaught Error: Call to a Member Function creatConsult() on null in /srv/disk4/2733175/www/netcodes.cf/techesc/classes/professor.class.php:42
Stack trace:
0 . /scripts/processes.php(57): Professor->getMaxID()
1 {main} thrown in ./classes/professor.class.php on line 42

professor.class.php `

require_once "conexao.class.php";
require_once "pessoa.class.php";

class Professor extends Pessoa
{
    private $hab1, $hab2, $hab3, $estadoCivil;
    private $banco;

    function __construct()
    {
        $this->banco = new Conexao();
    }

    //~~ Funções ou Metodos
    public function getMaxID(){
        $sql = "SELECT MAX(ID) FROM PROFESSOR WHERE CODESCOLA = ". $_SESSION["id"];
        $rs = $banco->criarConsulta($sql);
        if($row = mysqli_fetch_row($rs))
            return $row[0];
        else 
            return "0";
    }
} `    

pessoa.class.php `

require_once "conexao.class.php";
class Pessoa
{
    private $id, $nome, $nasc, $fone, $cidade, $end, $cep, $uf, $foto;

    // function __construct()
    // {

    // }

    //Getters and Setters
    public function getId(){
        return $this->id;
    }
    public function setId($id){
        $this->id =  $id;
    }
    public function getNome(){
        return $this->nome;
    }
    public function setNome($nome){
        $this->nome =  $nome;
    }
}`  

conexao.class.php

class Conexao{

private $local, $user, $senha, $banco;
public $comandoSql, $result, $con;

function __construct(){
    $this->local = "example.com";
    $this->user = "123";
    $this->senha = "123";
    $this->banco = "banco";
}

public function criarConsulta($sql){
    $this->conectarBD();
    $this->comandoSql = $sql; 

    if($this->result = mysqli_query($this->con,$this->comandoSql)){
        return $this->result;
        $this->desconectarBD();
    }else{
        echo "Problemas ao realizar Comando SQL !";
        die();
        $this->desconectarBD();
    }
}}

Page php process. who calls the class

$prof = new Professor();
echo $prof->getMaxID();

1 answer

0


Missed putting the this on the call to the bank in:

//~~ Funções ou Metodos
public function getMaxID(){
    $sql = "SELECT MAX(ID) FROM PROFESSOR WHERE CODESCOLA = ". $_SESSION["id"];
    $rs = $banco->criarConsulta($sql);
    if($row = mysqli_fetch_row($rs))
        return $row[0];
    else 
        return "0";
}

Should be

//~~ Funções ou Metodos
public function getMaxID(){
    $sql = "SELECT MAX(ID) FROM PROFESSOR WHERE CODESCOLA = ". $_SESSION["id"];
    //     vvvv Faltou o this
    $rs = $this->banco->criarConsulta($sql);
    //     ^^^^ Faltou o this
    if($row = mysqli_fetch_row($rs))
        return $row[0];
    else 
        return "0";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.