Forum Moderators: coopster

Message Too Old, No Replies

code order in PHP 4 class

Attempting to assign function variables to class variables

         

PHP_Chimp

2:50 pm on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have been having issues with trying to pass a variable to another variable. I have now found the problem, but dont understand why both versions dont work.

It is a PHP 4 class where one of the class variables is $name.

I have a function that has a $_POST variable in that I want to assign to $this->name;

This works as expected -
$this->name = $n; ($n is the $_POST variable)

This doesnt work -
$n = $this->name;

I cant work that out...maybe its just me not looking properly but if there is a good reason why could someone let me know.

Thanks

coopster

3:33 pm on Jul 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, PHP_Chimp.

Where are you trying to assign that value? Scope is the likely issue.

PHP_Chimp

3:59 pm on Jul 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



class Someclass {

var $name;

function func($n, $p){
$this->name = $n;
... rest of the code

It is all in the same class. Nothing out of the ordinary.

coopster

6:34 pm on Jul 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Works fine for me:
class Someclass { 
var $name;
function func($n, $p)
{
$this->name = $n;
print $this->name . "<br>\n";
$this->name = 'NameNew';
$n = $this->name;
print $n . "<br>\n";
}
}
$myClass = new Someclass();
$myClass->func('NameN', 'NameP');
exit;

Prints:
NameN 
NameNew

PHP_Chimp

2:12 pm on Jul 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ye as i posted it works. Just wanted to know why it doesnt work as $n = $this->name;

As the manual says that it doesnt matter what order variables are declared in i.e. ($a = $b) === ($b = $a). Just in this case $n = $this->name doesnt work...

Just wanted to know if this was something that others had experienced or if it is just a bug for me, or if i am missing something in the manual.

coopster

3:27 pm on Jul 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Just wanted to know why it doesnt work as $n = $this->name;

It does. Look again at the example posted.

I must be missing something here. Post a small example of your code and the error you are receiving (or explain in more detail what "doesn't work" means).

PHP_Chimp

4:10 pm on Jul 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wasnt receiving an error just wasnt getting the variable passed through.

Seeing as it worked for you im just guessing that there is something wrong with my set up. As i am getting the correct rosponce for $this->name = $n and not for $n = $this->name.

Was really just trying to find out if i was going mad or found some 'bug' with my set up.

Seeing as it is in PHP 4 and they are stopping it guess that in the long run it doesnt matter :)

coopster

8:02 pm on Jul 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't think it is your setup, I think it is the code. Feel free to post a small example here and we'll help you get it figured out ;)

darrenG

2:01 pm on Jul 15, 2007 (gmt 0)

10+ Year Member



The way I read your problem is that you have misunderstood the assignment of variables. The variable left of the = is assigned the value of the variable to the right of the =.

$this->n = $n;

and

$n = $this->n;

Are not interchangable, they do different things

<edit>

To use your example:

($a = $b) === ($b = $a) is not correct.

($a = $b)!== ($b = $a) is correct.

If Ive misunderstood I apologise for sucking eggs!

[edited by: darrenG at 2:06 pm (utc) on July 15, 2007]