1
In performing deploy of my files, I noticed that some functions that were working normally on the localhost (local server) did not behave as expected on the hosting server, more specifically when I need to use the Classes that I created. The server generated an error file, error_log.
Filing cabinet error_log: Classes I use in this operation, Products.php:
<?php
require_once "Crud.php";
class Produtos extends Crud {
private $tabela;
public function __construct() {
parent::__construct();
$this->tabela = "produtos";
}
public function store($params, $values) {
parent::store($this->tabela, $params, $values);
}
public function index($column, $restriction) {
return parent::index($column, $this->tabela, $restriction);
}
public function show($column, $restriction) {
return parent::show($column, $this->tabela, $restriction);
}
public function update($setters, $values) {
parent::update($this->tabela, $setters, $values);
}
public function excluir($restriction, $values) {
parent::excluir($this->tabela, $restriction, $values);
}
}
?>
Crud.php:
<?php
require_once "Conexao.php";
class Crud extends Conexao {
public function __construct() {
parent::__construct();
parent::conectar();
}
public function store($tabela, $params, $values) {
$sql = "INSERT INTO $tabela VALUES($params)";
if (parent::executarSql($sql, $values)) {
echo "<script>alert('Produto cadastrado com sucesso!');window.location.href='../../../addproduto.php'</script>";
} else {
echo "<script>alert('Falha ao cadastrar produto, contate o administrador!');window.location.href='../../../addproduto.php'</script>";
}
}
public function index($column, $tabela, $restriction) {
$sql = "SELECT DISTINCT $column FROM $tabela $restriction";
return parent::executarFetchAll($sql, $restriction);
}
public function show($column, $tabela, $restriction) {
$sql = "SELECT DISTINCT $column FROM $tabela $restriction";
return parent::executarFetchAll($sql, $restriction);
}
public function update($tabela, $setters, $values) {
$sql = "UPDATE $tabela SET $setters";
if (parent::executarSql($sql, $values)) {
echo "Update efetuado com sucesso!";
} else {
echo "Falha ao realizar update!";
}
}
public function excluir($tabela, $restriction, $values) {
$sql = "DELETE FROM $tabela $restriction";
if (parent::executarSql($sql, $values)) {
echo "Exclusão efetuada com sucesso!";
} else {
echo "Falha ao realizar exclusão!";
}
}
}
?>
Connexion.php:
<?php
class Conexao {
private $usuario;
private $senha;
private $bd;
private $servidor;
private $conexao;
public function __construct() {
$this->usuario = "*****";
$this->senha = "******";
$this->bd = "*****";
$this->servidor = "******";
}
public function conectar() {
$this->conexao = new PDO(
"mysql:host=$this->servidor;dbname=$this->bd",
$this->usuario,
$this->senha,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
}
public function executarSql($sql, $valores) {
$comando = $this->conexao->prepare($sql);
foreach ($valores as $indice => &$valor) {
$comando->bindParam($indice, $valor);
}
return $comando->execute();
}
public function executarFetchAll($sql, $valores) {
$comando = $this->conexao->prepare($sql);
if ($valores != null) {
foreach ($valores as $indice => &$valor) {
$comando->bindParam($indice, $valor);
}
}
$comando->execute();
return $comando->fetchAll();
}
}
?>
First of all, could justify why the class
Produtos
inherits from the classCrud
, who in turn inherits from the classConexao
? Are you familiar with the Liskov substitution principle? How would you evaluate this principle in your code?– Woss
I could use only the class
Crud
inheriting fromConexao
, but I decided to create the classProdutos
, because in the future I will add some more business rules that only apply to it. I do not know the principle quoted, I will research on.– Gabriel Ribeiro
The mistake is basically because you’re using inheritance where you shouldn’t use it, adding problems that shouldn’t exist. The Liskov principle will explain well why this situation should not be used inheritance, mainly concerning the error obtained. In this case it would make more sense to use composition between classes.
– Woss