Я пытаюсь создать экземпляр класса PHP, передавая параметры конструктору, но когда я печатаю данные, значения пустые.
Данные POST, переданные в configuracaoBancoDados.php
получено без проблем, но когда я создаю BancoDados class
от BancoDadosClass.php
, передайте параметры конструктору и попробуйте распечатать эти параметры, используя voltaValor() method
все данные пусты.
configuracaoBancoDados.php
<?php
include("../../../classes/BancoDadosClass.php");
if(isset($_POST["acao"]) && $_POST["acao"] == "criarBancoDados") {
$host = $_POST["enderecoServidor"];
$nomeBD = $_POST["nomeBD"];
$prefixoTabelasBD = $_POST["prefixoTabelasBD"];
$usuarioBD = $_POST["usuarioBD"];
$senhaBD = $_POST["senhaBD"];
$bancoDados = new BancoDados($host, $usuario, $senhaBD, $nomeBD, $prefixoTabelas);
echo $bancoDados->voltaValor();
} else {
echo "Ação não definida";
}
?>
BancoDadosClass.php
<?php
class BancoDados {
var $host;
var $usuario;
var $senha;
var $nomeBancoDados;
var $prefixoTabelas;
var $conexao;
function __construct($hostBD, $usuarioBD, $senhaBD, $nomeBD, $prefixoTabelasBD) {
$this->host = $hostBD;
$this->usuario = $usuarioBD;
$this->senha = $senhaBD;
$this->nomeBancoDados = $nomeBD;
$this->prefixoTabelas = $prefixoTabelasBD;
}
function voltaValor() {
return "Dados: " . $host . " " . $nomeBancoDados . " " . $prefixoTabelas . " " . $usuario . " " . $senha;
}
function conectar() {
$retorno = true;
$this->conexao = mysqli_connect($host, $usuario, $senha);
if(!$this->conexao) {
$retorno = false;
}
return $retorno;
}
function desconectar() {
mysqli_close($this->conexao);
}
}
?>
Вы должны напечатать так
function voltaValor()
{
return "Dados: " . $this->host . " " . $this->nomeBancoDados . " " . $this->prefixoTabelas . " " . $this->usuario . " " . $this->senha;
}
Для того, чтобы получить доступ к свойство экземпляра объекта изнутри объекта, вы должны использовать $this->whateverTheNameOfTheVariable
,
Для справки смотрите:
Других решений пока нет …