?
Solved

OO PHP question

Posted on 2005-03-09
15
Medium Priority
?
315 Views
Last Modified: 2008-03-10
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
Comment
Question by:gruntar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13495310
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
 
LVL 9

Author Comment

by:gruntar
ID: 13495391
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
 
LVL 5

Expert Comment

by:aksteve
ID: 13495491
Add a new variable called array (or whatever)

$this->array[$kc] = $vc;
$this->$xkey = $this->array;
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 9

Author Comment

by:gruntar
ID: 13495669
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
 
LVL 5

Expert Comment

by:aksteve
ID: 13495764
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
 
LVL 1

Expert Comment

by:pikachusalad
ID: 13496133
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
 
LVL 1

Expert Comment

by:pikachusalad
ID: 13496333
...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
 
LVL 5

Expert Comment

by:aksteve
ID: 13496462
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
 
LVL 1

Expert Comment

by:pikachusalad
ID: 13496643
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
 
LVL 9

Author Comment

by:gruntar
ID: 13496874
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
 
LVL 1

Accepted Solution

by:
pikachusalad earned 1200 total points
ID: 13497030
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
 
LVL 1

Expert Comment

by:pikachusalad
ID: 13497073
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
 
LVL 5

Assisted Solution

by:aksteve
aksteve earned 800 total points
ID: 13497114
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
 
LVL 5

Expert Comment

by:aksteve
ID: 13497151
Or actually this would work. It isn't a variable variable persay..but it will eval $xkey seperate

$this->{$xkey}[$kc] = $vc;
0
 
LVL 9

Author Comment

by:gruntar
ID: 13497402
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question