Instantiating one class in another constructor in PHP?

-1

I have a class named teacher that is heiress of another person call, I am trying to instantiate a third class of connection with bank in the class constructor method of Teacher, however it is returned to me

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

teacher.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";
    }
} '    

person.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;
    }
}'  

connection.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();
    }
}}

Process.php page that calls the class

$prof = new Professor();
echo $prof->getMaxID();
    
asked by anonymous 12.06.2018 / 14:17

1 answer

0

Failed to place this in call to bank at:

//~~ 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";
}
    
12.06.2018 / 14:22