Is it possible to create a private or protected class in PHP in order to allow access to its variables and functions only to other specific classes?
Application: I have a class where I create a connection to the database and wanted to allow access to this class only to other classes that perform CRUD in the database
connection.php
<?php
class Conexao extends Mysqli {
private static $conexao = null;
function Conexao($servidor, $usuario, $senha, $banco) {
parent::__construct($servidor, $usuario, $senha, $banco);
}
public function __destruct() {
self::$conexao->close();
}
public static function getConexao() {
if(!isset(self::$conexao)){
self::$conexao = new Conexao("localhost", "usuario", "senha", "nome_banco");
if (mysqli_connect_error()) {
die('Erro ao conectar ao banco de dados (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
if (false === self::$conexao->set_charset('utf8')) {
die("Error ao usar utf8");
}
}
return self::$conexao;
}
}
user.php
<?php
require_once "conexao.php";
class DAO_usuario {
private $conexao = null;
function __construct() {
$this->conexao = Conexao::getConexao();
}
public function cadastrar_usuario($nome, $usuario, $senha, ...) {
// [...]
}
}
Note: I do not use nor will I use frameworks , pure PHP only
The focus of the question is OOP, but it would be interesting to comment on the procedural style equivalent