![](/img/trans.png)
[英]How can I find the instance calling another Magento object's method from within that object?
[英]How can I access instance properties from within the object?
我试图实例化一个将参数传递给构造函数的PHP类,但是当我打印数据时,这些值是空的。
毫无问题地接收传递到configuracaoBancoDados.php
的POST数据,但是当我从BancoDadosClass.php
创建BancoDados class
,将参数传递给构造函数,并尝试使用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
。
供参考,请参阅:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.