Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

OO PHP question

I have create a class that have predefined variables.

    $this->msg = array('auth_valid' => 'User "%s" valid.',
                       'err_pass' => 'Enter valid pass "%s"',
                       'err_somevar' => 'Error please...');


Now I have a function that can set variables...

function set_var($xkey, $xval){
      
      if(is_array($xval))
      {
            foreach ($xval AS $kc => $vc)
            {
                  $this->$xkey[$kc] = $vc;
            }      
      }
      else
      {
            $this->$xkey = $xval;
      }
      
}


$userauth = new authclass();
// works
$userauth->set_var('msg', 'value of var');
//does not set values
$userauth->set_var('msg',array('auth_valid' => 'User %s authenticated',
                           'new_val' => 'some msg here'));


Function does set variable if value is not an array. So where is the problem?

Cheers
0
gruntar
Asked:
gruntar
  • 5
  • 5
  • 4
  • +1
2 Solutions
 
Marcus BointonCommented:
Kind of ugly, but you could do it recursively inside the loop:

$this->set_var($kc, $vc);

If you're in PHP5, have you looked at using __set()?
0
 
gruntarAuthor Commented:
Nope, I'm still using 4.x

The weird thing is that I can set var like this
$this->$xkey = $xval;
but not as array
$this->$xkey[$kc] = $vc;

any ideas?
0
 
aksteveCommented:
Add a new variable called array (or whatever)

$this->array[$kc] = $vc;
$this->$xkey = $this->array;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gruntarAuthor Commented:
Nope, this is no good. Hence I already have an array when calling function. Function must not unset any pre-devined values.

// pre-defined values
    $this->msg = array('auth_valid' => 'User "%s" valid.',
                       'err_pass' => 'Enter valid pass "%s"',
                       'err_somevar' => 'Error please...');


Now, Let say, I just need to change text of first value. So I call function

$userauth->set_var('msg',array('auth_valid' => 'User %s authenticated'));

All other vars err_pass, err_somevar.. must stay intact. That is why i'm setting ecah value in a loop. So that only requested variables get changed.

any more ideas?!
0
 
aksteveCommented:
change:$this->$xkey = $this->array;

to:$this->$xkey = array_merge($this->$xkey,$this->array);

You need to test to make sure $this->$xkey is an array first
0
 
pikachusaladCommented:
Why not just change your function to:

function set_var($xkey, $xval){    
  $this->$xkey = $xval;
}

You'll end up with:

 ["msg"] => array(2) {
  ["auth_valid"] => string(21) "User %s authenticated"
  ["new_val"] => string(13) "some msg here"
 }
0
 
pikachusaladCommented:
...sorry (why don't *I* read the whole question before trying to answer)

function set_var($xkey, $xval){
      if(is_array($xval)) {
            if (isset($this->$xkey) && is_array($this->$xkey)) {
                  $temp_array = $this->$xkey;
                  foreach ($xval AS $kc => $vc)      {
                     $temp_array[$kc] = $vc;
                  }
                  $this->$xkey = $temp_array;
            } else {
                  $this->$xkey = $xval;    
            }
      } else {
            $this->$xkey = $xval;
      }
}
0
 
aksteveCommented:
You should try reading the other answers as well..lol.. You nearly copied my first solution(which was wrong because it replaced the array)..

:)
0
 
pikachusaladCommented:
Mine doesn't replace the array.

$userauth->set_var('msg',array('a' => 'dog', 'b' => 'cat'));

will give you:

object(authclass)(1) {
 ["msg"] => array(2) {
  ["a"] => string(3) "dog"
  ["b"] => string(3) "cat"
 }

$userauth->set_var('msg',array('a' => 'doggy', 'c' => 'squirrel'));

will give:

object(authclass)(1) {
 ["msg"] => array(3) {
  ["a"] => string(5) "doggy"
  ["b"] => string(3) "cat"
  ["c"] => string(8) "squirrel"
 }

Which makes changes, adds new, and preserves old.

0
 
gruntarAuthor Commented:
Hi, pikachusalad
well, workaround is working. But my question is why my initial code does not work? It's nothing wrong with it.
Any clues?
0
 
pikachusaladCommented:
because $xkey = 'msg'

and $xkey[<string>] evaluates to $xkey[0], which is the first character of 'msg', 'm'

so, you're setting $this->m = $vc
0
 
pikachusaladCommented:
let me try to make that more clear...

IF...

$xkey = 'msg';

$xval = Array('a' => 'b');

If you try to set:

$this->$xkey['a'] = 'b';

PHP evaluates $xkey['a'], not just $xkey

So $this->$xkey['a'] is actually $this->m, because of what I said in the last comment.
0
 
aksteveCommented:
Hmm so would $this->${$xkey}[$kv] work? I haven't tried but that will make it eval $xkey alone. A variable variable array..ugh..
0
 
aksteveCommented:
Or actually this would work. It isn't a variable variable persay..but it will eval $xkey seperate

$this->{$xkey}[$kc] = $vc;
0
 
gruntarAuthor Commented:
Thanks for explanation pikachusalad, and well, aksteve you're just lucky to post a good tip (based on pikachusalad's explanation) before I closed the question.

Thanks
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now