Jump to content

question about class


hno

Recommended Posts

 

HI

i'm is learning about using calss in php.I wrote this code:

class Person {

function __construct($name)

{

$this->name = $name;

 

}

function getName()

{

return $this->name;

}

private $name;

};

$a= new Person("a") . "\n";

print $a->getName();

 

When I using this code ,it gives me the following error:

"Catchable fatal error: Object of class Person could not be converted to string in C:\wamp\www\class\class.php on line 14"

 

but when I change it to following ,it doen't have any errors and show me the result.

 

class Person {

function set($name)

{

$this->name = $name;

 

}

function getName()

{

return $this->name;

}

private $name;

};

$a = new Person;

$a->set("a");

print $a->getName();

 

What is the problem?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/145371-question-about-class/
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.