Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem using classes OOP 1

Status
Not open for further replies.

doctorit

Programmer
Aug 17, 2006
5
IR
while i was testing php OOP features ....

try this code:

<?
class class1{
function quote(){
echo("says:'");
}
}

class class2{
function say($name,$object,$string){
print($name.$object->quote().$string."'.");
}
}
$obj1=new class1();
$obj2=new class2();
$obj2->say("descartes ",$obj1,"i think therefore i am");

?>


you'll get this output:

says:'descartesi think therefore i am'.

but the desired output is:

descartes says : ' i think therefore i am '.

that simply can be achieved by this code :

<?
class class1{
function quote(){
echo("says:'");
}
}

class class2{
function say($name,$object,$string){
print($name);
print($object->quote());
print($string."'.");
}
}
$obj1=new class1();
$obj2=new class2();
$obj2->say("descartes ",$obj1,"i think therefore i am");

?>


But what is the problem in first code

 
what is happening is that the method of class1 is being called as the second argument of an echo/print. the print command does not flush to the browser until it is completed but the middle argument does not result in a return value but actually outputs text to the browser using echo and so actually executes and completes before the print command from which it is called.

so... solutions:
1. change the echo to a return
Code:
 return "says: '";
or
2. change the say function to
Code:
		print $name;
		$object->quote();
		print $string."'.";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top