Page 1 of 1

Having problems passing a value into a class

Posted: Wed Apr 13, 2005 8:37 am
by icarpenter
Hi

I am trying to display the value of variable $test in 2 functions simultaneously that reside in the same class to no avail...I am using PHP 4
Can anyone help...

PLease see code below...

Thanks Ian.

Code: Select all

///////////////// Code Calling the class class.php ///////////////

$a = new a($x=a);
$b = new b($x=b);

$a->test ='I am trying to display this in text in function a and b simultaneously please help me i\'m desperate!!!';

// Class.php *******

class a {

    var $test='default';

    function a($x)
    {
        echo"$x\n";
        echo"$this->test\n";
    }
} 

class b extends a {

    function b($x)
    {
        echo"$x\n";
        echo"$this->test\n";
    }
}

Posted: Wed Apr 13, 2005 9:58 am
by pickle
Well, a couple of things:
when creating your objects, you don't need to assign a value to $x. You can create the object like this:

Code: Select all

$a = new a('a');
Putting '$x=a' in those parenthesis probably gave you an error because 'a' needs to be quoted.

The way your classes are set up, they will print out whatever is sent when creating the object. So, if you want them to display that long string, just send it along as a parameter when creating the object:

Code: Select all

$test = 'I am trying ...';
$a = new a($test);
$b = new b($test);
If you're trying to assign what is passed when you create the object, to the local object variable 'test', you need to do that assignment when in the creator function:

Code: Select all

class a
{
 var $test = 'default';
 
 function a($x)
 {
  $this->test = x;
  echo $this->text;
 }
}
From the looks of your code, it appears that you're just starting to learn how to use objects. I'd suggest looking for tutorials or some online guides to setting up objects - just to get the basic concepts down.

Posted: Wed Apr 13, 2005 10:28 am
by icarpenter
Pickle thanks for you reply you would be right I am new to OO...I think my problem is more general though. I am trying to assign a value to all functions within a class without having to assign a value to each function individually!!!

I can do this manually by changing the attribute within the class itself...

Code: Select all

var $test='Ohh my god it works when I change this!';
Within the class itself, But I cannot do it from a single call...ie:-

Code: Select all

$a = new a('I know this wrong as it only prints text from function a')
Can you work some magic Pickle???

Posted: Wed Apr 13, 2005 11:31 am
by SystemWisdom

Code: Select all

$a = new a('a');
$b = new b('b');
 
$b->SetText( 'I am trying to display this in text in function a and b simultaneously please help me i\'m desperate!!!' );
 
echo $a->GetText();
echo $b->GetText();


// Class.php *******
 
class a {
 
    var $test;
 
    function a($x)
    {
        $this->test = $x;

        echo "$x\n";
        echo "$this->test\n";
    }

    function SetText( $x )
    {
        $this->test = $x;
    }

    function GetText()
    {
        return $this->test;
    }
} 
 
class b extends a {
 
    function b($x)
    {
        $this->a( $x );  // Call base constructor
    }
}
I hope that helps!!

Posted: Wed Apr 13, 2005 1:04 pm
by pickle
When you assign a variable using $this->variable_name, the '$this' refers to the object. So, if you assign a value to '$this->test' in the creator function, the variable 'test' is set for that object. You can then access it from any function in that object, as the scope of '$this' type variables are the whole object.

~SystemWisdom's code shows this. He uses the 'SetText' function to set an object variable ($this->test). He then access that variable from another function, but inside the same class, 'GetText'.

Anytime you set a variable with '$this->', you refer to the object variables. When you deal with a variable (such as $x in the examples), that is NOT an object variable, so it's scope is just the funciton its in

Posted: Thu Apr 14, 2005 2:50 am
by icarpenter
Many thhanks for your replies but I still dont get it... the result I got running the previous code didn't display the text twice.

Please see below I have a recreated the problem in a different way...

Code: Select all

<pre>
<?php

$a = new a('Class A...');
$b = new b('Class B...');

// Classes *******

class a
    {

    var $test='1';
    var $width='200';

    function a($x)
    	{
        echo"<table border='$this->test' width='$this->width'><tr><td>$x</td></tr>\n";
    	}
    }

class b extends a
    {

    function b($x)
    	{
        echo"<table border='$this->test' width='$this->width'><tr><td>$x</td></tr>\n";
    	}
    }
?>
</pre>
If I manually change the value of:

Code: Select all

var $test='1';
To

Code: Select all

var $test='0';
The border switches off in both Class A and B simultainously...

but how to I do I change the value of $test from a single call?

Posted: Thu Apr 14, 2005 9:56 am
by pickle
So, you want to change the value of the $test variable in both your $a & $b objects in a single call? I don't think that can be done once the objects are created.

If it absolutely had to be done with a single call from the code you've already got, you could probably pass both the objects to another function, and have that function call a "change_test()" type function on both $a & $b seperately.

Or you could just embed that code in what you've already got.

Or you could put what you want $test to be in a global (shudder!) variable, and have both of the objects refer to that global variable. That would allow you to at least dynamically change the value of $test before the objects are created.

Posted: Thu Apr 14, 2005 11:34 am
by icarpenter
Thanks Pickle...Thats got it! using a global variable that is!

Many Thanks for all for your help...