Class code: problem with $this...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Class code: problem with $this...

Post 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!
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

$this is a reserved variable and can only be used INSIDE the class for referencing the class object.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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';
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
Jixxor
Forum Commoner
Posts: 46
Joined: Wed Jun 07, 2006 5:53 pm
Location: Lakeland, FL

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

Post 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.
Post Reply