php - How can I access instance properties from within the object? -


i trying instantiate php class passing parameters constructor when print data values empty.

the post data passed configuracaobancodados.php received without problems when create bancodados class bancodadosclass.php, pass parameters constructor , try print parameters using voltavalor() method data empty.

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);   } }  ?> 

enter image description here

you have print this

function voltavalor()  {     return "dados: " . $this->host . " " . $this->nomebancodados . " " . $this->prefixotabelas . " " . $this->usuario . " " . $this->senha; } 

in order access instance property of object within scope of object, need use $this->whateverthenameofthevariable.

for reference, see:


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -