1

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

5
  • No need to echo var_dump(), just var_dump(). Commented Jul 13, 2016 at 1:23
  • You should read this one... stackoverflow.com/questions/797380/…. Also as a note it is much more preferable to copy and paste code rather then screenshots. Also as that answer saws using global variables is super bad practice. Commented Jul 13, 2016 at 1:28
  • You should post your code rather than images. Don't post the whole script though, just the excerpts that are needed for use to understand the problem. Commented Jul 13, 2016 at 1:42
  • i arrange post scripts Commented Jul 13, 2016 at 1:59
  • Please see the post again Commented Jul 13, 2016 at 2:21

1 Answer 1

0

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.

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

Note that your require_once("../idioma/lang.br.php") inserts the $user array above the class definition. Your problem has to do with variable scope. When you define the array outside of your class, the class members cannot see it

$user = [...];
Class UsarioBLL{
// $user is undefined here.
}

To gain visibility, you need to pass the data and store it in a class variable. The simplest way is in my original answer above. Another way is to pass data to the class constructor:

Class UsarioBLL{
    private $user;
    __construct($input_data){this->user = $input_data}
}

__construct is called when a new object is created. So create your new object like so:

$usario = new UsarioBLL($user); //will call __construct and pass $user
Sign up to request clarification or add additional context in comments.

4 Comments

yes I can pass the array as a parameter but because "class.usuario.dao.php" include working? below the line of $dao = new UsuarioDAO(); It does not display error should not this also presenting error?
Please see the post again
Thanks for cleaning up your question. I've edited my answer
Work .... Class UsuarioBLL { public function CadastraUsuario($campos) { require_once("../idioma/lang.br.php"); $msg = array(); // senha if(strlen($campos['senha']) >= 6 && strlen($campos['senha']) <= 30) array_push($msg,$user["TAMANHO_SENHA"]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.