Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Need help use echo or return statement


fcab

Recommended Posts

Posted

I am not sure how to use the return statement for the code below. It works fine with echo.

I do not think echo should be use in the code below. I am new to php and a little help wold be great.

 

 

Thanks,

Chris

 

class myclass {

var $variable = 'test';

 

 

function myclass($TEST = 'It') {

 

$TEST .= ' Works';

 

// Can you use echo or print in the statement below. If not how do you use the return statement to out put "It Works"

 

///////////////

echo $TEST;///

//////////////

}

}

 

new myclass();

Posted

echo is fine if you always want to output the results of the function at object creation. Another way to get the same effect is to assign the value to a class variable and create a return function for the class variable:

class myclass {
 var $variable = 'test';

 function myclass($TEST = 'It') {

   $TEST .= ' Works';

   $this->variable = $TEST;

   return $this;
 }

 function get_variable() {
   return $this->variable;
 }
}

$my_obj = new myclass();
echo $my_obj->get_variable();

This way, you could create the object at one place; work on it elsewhere; then finally display it.

 

Hth,

Matt

Always back up before making changes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...