Log in

View Full Version : echo nothing?



begeiste
11-21-2011, 10:56 PM
Hi,
I have created a class "friendClass" and tried to echo it. It prints nothing without any errors, not sure what was it going on? Any helps would be appreciated!

class friendClass{

//attributes - variable
public $name;
public $sex;
public $title;
public $country;

//methods - function
public function eat(){}
public function watch(){}
public function drive(){}
public function speak(){}

}

$friend1 = new friendClass;
$friend2 = new friendClass;
$friend3 = new friendClass;
$friend4 = new friendClass;

$friend1 -> name = "Bach";
$friend1 -> title = "Muscian";
$friend1 -> sex = "Male";
$friend1 -> country = "Germany";

$friend1 -> eat("Bread");
$friend1 -> speak("German");
$friend1 -> drive("Horse");
$friend1 -> watch("Beethoven");

echo "Name is: ".$friend1->name;

djr33
11-22-2011, 05:33 AM
See traq's post below. I misread the code.
Two things:
1. Your class is not closed at the end. Add a } if you don't have one. If not, post the whole code so we can help. If you don't have a final bracket, this probably is why nothing is happening: you have a parse error, and your error reporting (at the system level) is not on so that you just see a blank page.
2. That won't echo anything (or do anything at all) until you create an instance of the class. For example: $myinstance = new friendClass();.
Also, it's somewhat odd to use an echo statement like that in the definition of a class. It's probably better to put that in the __create() function, which is run by default when a class is created.

traq
11-22-2011, 04:19 PM
won't echo anything (or do anything at all) until you create an instance of the class.

actually, that's what it is: you aren't instantiating the class.
// this
$friend1 = new friendClass;

// should be
$friend1 = new friendClass();

however, Daniel is right that you should be getting some errors from that code. you should check your error reporting settings.

your methods are empty, so they won't do anything at all. But when you try to pass an argument to them when they don't expect any, you should be getting an error.

djr33
11-22-2011, 08:02 PM
Oh, I completely missed that. I must have been too tired when I read that. Thanks for pointing it out.