Page 1 of 1

Class code: problem with $this...

Posted: Wed Jun 28, 2006 6:08 pm
by mattcooper
Hi guys,

I have written a class that should display a table containing Flash elements and images for a game framework, but am getting no output for the elements using this code:

Code: Select all

$game = new Game;

$game->title_image = "playgame_r1_c1.gif"; // etc...
Here's the function code for the display table:

Code: Select all

function table() { // Make the table for the game to display in
	
	function display_Title() { // output the title image
		echo "<img src = \"images/" . $this->title_image . "\" width = \"310\" height = \"104\" alt = \"Title image\">\n";
	}
		
	function set_Credits() {
	if(!isset($_SESSION['credits_left'])) {
		$_SESSION['credits_left'] == 3;
	}
	// create the Flash object
	echo "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"190\" height=\"104\">";
    echo "<param name=\"movie\" value=\"flash/credits.swf?credits=" . $this->credits_left . "\">";
    echo "<param name=\"quality\" value=\"high\">";
    echo "<embed src=\"flash/credits.swf?credits=" . $this->credits_left . "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"190\" height=\"104\"></embed>";
    echo "</object> ";
	
		}
	function play_Image() {
			echo $this->play_image;
		}

	function gamble_Image() {
			echo "<img src = \"" . $this->gamble_image . "\">";
		}
	
	function intro_Movie() {
			echo "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"500\" height=\"236\">";
 			echo "<param name=\"movie\" value=\"flash/intro.swf?" . $this->game_explanation . "\">";
  			echo "<param name=\"quality\" value=\"high\">";
			echo "<param name=\"LOOP\" value=\"false\">";
	    	echo "<embed src=\"flash/intro.swf?" . $this->game_explanation . "\" width=\"500\" height=\"236\" loop=\"false\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\">";
			echo "</embed>";
			echo "</object>";
		}
	
	echo "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" width=\"500\">
  			<tr>
   			<td><img src=\"images/spacer.gif\" width=\"310\" height=\"1\" border=\"0\" alt=\"\"></td>
   			<td><img src=\"images/spacer.gif\" width=\"190\" height=\"1\" border=\"0\" alt=\"\"></td>
   			<td><img src=\"images/spacer.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>
  			</tr>
			<tr>
   			<td bgcolor=\"#009900\">"; display_Title(); echo "</td>
   			<td bgcolor=\"#009900\">"; set_Credits(); echo "<div align=\"center\">
   			</div></td>
   			<td><img src=\"images/spacer.gif\" width=\"1\" height=\"104\" border=\"0\" alt=\"\"></td>
  			</tr>
  			<tr>
    		<td colspan=\"2\" bgcolor=\"#009900\">"; intro_Movie(); echo "</td>
    		<td><img src=\"images/spacer.gif\" width=\"1\" height=\"236\" border=\"0\" alt=\"\"></td>
  			</tr>
  			<tr>
   			<td colspan=\"2\" bgcolor=\"#009900\">"; play_Image(); gamble_Image(); echo "<div align=\"center\"></div></td>			
   			<td><img src=\"images/spacer.gif\" width=\"1\" height=\"60\" border=\"0\" alt=\"\"></td>
  			</tr>
		</table>";
	}
If I instantiate the class using the first bit of code, I can output info by using:

Code: Select all

echo $game->title_image; // etc...
but it fails if I use $this->title_image

Why would that be?

Thanks in advance!

Posted: Wed Jun 28, 2006 6:11 pm
by AKA Panama Jack
$this is a reserved variable and can only be used INSIDE the class for referencing the class object.

Posted: Wed Jun 28, 2006 6:15 pm
by Chris Corbyn

Code: Select all

function table() { // Make the table for the game to display in
       
        function display_Title() { // output the title image
                echo "<img src = \"images/" . $this->title_image . "\" width = \"310\" height = \"104\" alt = \"Title image\">\n";
        }
You're putting functions inside functions?

Posted: Wed Jun 28, 2006 6:34 pm
by mattcooper
AKA Panama Jack wrote:$this is a reserved variable and can only be used INSIDE the class for referencing the class object.
I am only using $this inside the class, but am having problems getting info by using the $game variable!

Functions inside functions... yes, would that be contributing to the problem?

Thanks guys!

Posted: Thu Jun 29, 2006 1:33 am
by Chris Corbyn
mattcooper wrote:Functions inside functions... yes, would that be contributing to the problem?
No, you're just creating the function in function scope so it's only accessible to that function.... I was curious what you were trying to acheive there.

I'm not sure I'm clear what the actual problem is. Are you seeing any errors?

Posted: Thu Jun 29, 2006 1:48 am
by Weirdan
When you define function inside method, in fact you define stand-alone function (with no relations to the object it's being defined in). Thus you can't use $this variable from inside such a function.

Posted: Thu Jun 29, 2006 1:51 am
by Weirdan
d11wtq wrote:No, you're just creating the function in function scope so it's only accessible to that function....
Either I misunderstood your comment or you're wrong. Try this code:

Code: Select all

function a() {
        function b() {
            echo "inner func";
        }
    }
//  b(); // fails
    a();
    b(); // echoes 'inner func';

Posted: Thu Jun 29, 2006 2:42 am
by Chris Corbyn
Weirdan wrote:
d11wtq wrote:No, you're just creating the function in function scope so it's only accessible to that function....
Either I misunderstood your comment or you're wrong. Try this code:

Code: Select all

function a() {
        function b() {
            echo "inner func";
        }
    }
//  b(); // fails
    a();
    b(); // echoes 'inner func';
Hmm... I was wrong. I tried:

Code: Select all

class foo
{
    function test1()
    {
        function test2()
        {
            echo "test2 called...";
        }
        test2();
    }
}

$foo = new foo;
$foo->test1(); //test2 called...
$foo->test2(); //ERROR
That backs up what you said but it's not what I wrote in my last post ;)

Re: Class code: problem with $this...

Posted: Thu Jun 29, 2006 12:08 pm
by Jixxor
Ummm, wasn't his code suppose to be a class, not a function inside a function.
mattcooper wrote:Hi guys,

I have written a class that should display a table containing Flash elements and images for a game framework, but am getting no output for the elements using this code:
So should his new code for his class be:

Code: Select all

class table {
// all the code goes here....
// ....
}
Instead of his original:

Code: Select all

function table () {
// with all the function definitions defined and built as a class here
}
Seems like a simple fix to me, unless I'm completely missing the point here.