Page 1 of 1

Unexpected output from very simple class

Posted: Thu Jul 06, 2006 3:51 am
by mattcooper
Hi guys,

I'm just trying to get to grips with OO PHP, and am having trouble with the following very simpl class:

Code: Select all

class create_Table {
	
		var $width;
		var $height;
		var $border;
		var $rows;
		var $content;
		var $data;
		
		function create_Frame ( ) { // create the framework for the table
		
				echo "<table border = ". $this->border ." width = ". $this->width ."% height = ". $this->height .">\n";
			
				$num = 1;
				$rows = $this->rows;
			
				while ( $num <= $rows ) {
			
					echo "<tr><td>". $this->content ."</td><td>Function call: ". $this->add_Data ( ) ."</td></tr>\n";
				
					$num++;
				
				}
			
			echo "</table>\n";
			
		} // end of function create_Frame ( )
		
		function add_Data( ) { // Add some data to the cells calling this function
			
			echo $this->data;
   		
		}
		
	} // end of class
Using the following code to instantiate and outpt it

Code: Select all

$table2 = new create_Table ( );
	
	$table2->data = "Hello world";
	$table2->width = "100";
	$table2->height = "200";
	$table2->border = "2";
	$table2->rows = "4";
	$table2->content = "Second table";
	
	$table2->create_Frame ( );
produces the following HTML output:

Code: Select all

<table border = 2 width = 100% height = 200>
Hello world<tr><td>Second table</td><td>Function call: </td></tr>
Hello world<tr><td>Second table</td><td>Function call: </td></tr>
Hello world<tr><td>Second table</td><td>Function call: </td></tr>
Hello world<tr><td>Second table</td><td>Function call: </td></tr>
</table>
You will see from this that $table2->data is outputting in the wrong place. Why would that be? Also, I appreciate that there is no need for a function to echo out $data in this case, but am trying underpin the principal of using one.

Thanks in advance...

Posted: Thu Jul 06, 2006 4:48 am
by JayBird
Instead of

Code: Select all

function add_Data( ) // Add some data to the cells calling this function
{ 
			   
    echo $this->data;
	 
}
You should return the data, NOT echo it

Code: Select all

function add_Data( ) // Add some data to the cells calling this function
{ 
			   
    return $this->data;
	 
}

Posted: Thu Jul 06, 2006 4:50 am
by GM
I think it's because your "add_Data()" function is doing an echo instead of a return.

If you change the line

Code: Select all

echo $this->data;
to

Code: Select all

return $this->data;
it should work.

Posted: Thu Jul 06, 2006 4:51 am
by mattcooper
Perfect! Can you enlighten me as to why "return" should be used instead of "echo"?

Many thanks guys.

Posted: Thu Jul 06, 2006 4:51 am
by GM
Damn it.

Must type faster. :wink:

It's because you are doing your formatting in the calling line of code. Then you are entering a new function that has no knowledge of that formatting, and echoing something to the screen. Then, you are returning to the previous function, which continues with it's own echo statement.

You need to look at it from this point of view: Where is the first echo?

The first echo is in the add_Data function, because that gets called before the echo in which you are constructing the HTML.

Posted: Thu Jul 06, 2006 4:54 am
by JayBird
GM wrote:Damn it.

Must type faster. :wink:
...becuase on this line

Code: Select all

echo "<tr><td>". $this->content ."</td><td>Function call: ". $this->add_Data ( ) ."</td></tr>\n";
you are already echoing

So you dont need to echo again.

Im not 100% sure, but it must be something to do with the order in which the commands are executed.

The echo inside the function gets executed before the echo in the line i highlighted above.

Posted: Thu Jul 06, 2006 5:04 am
by mattcooper
Excellent bit of help from you both there, many thanks. Gonna be working on this for most of the day, I'm sure you'll be hearing from me again shortly!!

:D

Posted: Thu Jul 06, 2006 5:33 am
by technofreak
As GM said,

Code: Select all

echo "<tr><td>". $this->content ."</td><td>Function call: ". $this->add_Data ( ) ."</td></tr>\n";
When PHP processes this line, its starts substituting the variables with their values. Teh variables will cause mere substitution of their values within the 'echo' statement which prints the entire row. But, when the function call is being processed PHP encounters another echo statement instead of a return value. This causes PHP to process that statement instead of substituting the value. Hence the echo'ed value within the function gets pronted first and then PHP prints the main echoed statement.

It is better to use a return statement when you are trying to print any variable throughit. If just HTML tags are going to be printed, then you don't need a 'return' statement.

For example,

Code: Select all

<?php

function return_tags() { ?>

<tr><td>Key : </td><td>Value :</td></tr>

<?php } ?>
This function will just add the HTML code contained within it in the place where it is called. In my codes geenrally, I donot prefer to do

Code: Select all

echo "<p>Welcome</p>";


within functions or anywhere. I just close the earlier PHP tag with ?>, write the HTML code and then again continue with <?php tag. I always feel better to leave HTML independent and not inscribed within PHP tags.