Eduardo Eduardo - 4 months ago 14
PHP Question

php include_once not work in class

I'm working to convert a project to multilanguage
but I have problems with require_once("../idioma/lang.br.php"); below

require_once("../idioma/lang.br.php");
require_once("../classe/class.usuario.dao.php");

Class UsuarioBLL
{
public function CadastraUsuario($campos)
{
$msg = array();
// senha
if(strlen($campos['senha']) >= 6 && strlen($campos['senha']) <= 30)
array_push($msg,$user["TAMANHO_SENHA"]);

if ($campos['senha'] != $campos['csenha'])
array_push($msg, $user["SENHA_DIFERENTE"]);

$dao = new UsuarioDAO();


in

array_push($msg,$user["TAMANHO_SENHA"]);


the array variable

$user["TAMANHO_SENHA"]


is null

this is a include

<?php
//cadastro usuario
$user = array();
$user["PREENCHER_TUDO"] = "Por favor preencha todos os campos.";
$user["TAMANHO_SENHA"] = "Senha deve conter entre 6 a 30 digitos.";


if I do a var_dump ($ user); before class

require_once("../idioma/lang.br.php");
require_once("../classe/class.usuario.dao.php");
var_dump ($ user);
Class UsuarioBLL
{
public function CadastraUsuario($campos)
{
$msg = array();
// senha
if(strlen($campos['senha']) >= 6 && strlen($campos['senha']) <= 30)
array_push($msg,$user["TAMANHO_SENHA"]);

if ($campos['senha'] != $campos['csenha'])
array_push($msg, $user["SENHA_DIFERENTE"]);

$dao = new UsuarioDAO();


the system print

array(2) {
["PREENCHER_TUDO"]=> string(35) "Por favor preencha todos os campos." ["TAMANHO_SENHA"]=> string(39) "Senha deve conter entre 6 a 30 digitos."
}


the require_once("../classe/class.usuario.dao.php");

$dao = new UsuarioDAO();


its working

I did a test
I create

class UserLang {
public $user;
function __construct() {
$this->user["PREENCHER_TUDO"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx.";
}
}


and it works now

$e = new UserLang();
echo $e->user["PREENCHER_TUDO"];


whyyy with class work
and an array out of a class does not work?

I do not know what is happening

Thank you for your help

Answer

Your class cannot see what is outside. If you mean for the class to work on a specific user dataset, you should make it a class property. Here's the simplest way:

Class UsarioBLL{
    public $user; //needs to be set before other functions can use it
}

After you create the object, set the user

$usario = new UsarioBLL();
$usario->user = $user; //now  the class can see the user data!

After you've done this, your CastradaUsario function will see the data.