tags:

views:

145

answers:

5
class hello {

  var $fname;
  var $lname;

  function attrib() {
    $this->fname = "sarmen";
    $this->lname = "dijango";
  }

  function say() {
    $name = $this->fname.", ".$this->lname;
    return $name;
  }
}

this is my sample class when i include the class on a page and type something like this

$d = new hello();
$d->say()

nothing happends. what am i doing wrong?

thanks

+4  A: 

"say" returns a string. You don't actually do anything with it. Shouldn't you print it or something?

Paul Tomblin
+7  A: 

You're treating the method attrib as if it was a constructor, but you need to name it __construct for it to behave as such.* Otherwise, you need to do this:

$myobject = new hello(); // create new object
$myobject->attrib(); // set the vars
print $myobject->say();

It is more common, however, to see something like this:

$myobject = new hello('my first name', 'my last name');
print $myobject->say();

And then instead of the attrib method you would have something that looks like this:

function __construct($fname, $lname) {
   $this->fname = $fname;
   $this->lname = $lname;
}

* Unless you are using PHP4, in which case you need to name your constructor the same as the class name.

Paolo Bergantino
The constructor is missing, but you should get at least a comma out of it anyway, right?
Martinho Fernandes
What do you mean?
Paolo Bergantino
He means that if you leave his class as is calling print $myobject-say() should output a single comma because $name will be populated with that comma he has in there. This is true.
gaoshan88
Well he still wouldn't see anything because he's not calling it with 'print' but I guess if he had called it correctly, yeah.
Paolo Bergantino
+1  A: 

Try going:

$var=$d->say();
echo $var;
Sam152
+4  A: 

Try this:

$d = new hello();
$d->attrib();
print $d->say();

Beyond that, you could make a few improvements such as this:

class hello {

   var $fname; 
   var $lname;

   public function __construct() {

      $this->fname = "sarmen"; 
      $this->lname = "dijango";

   }

  public function say() {

      $name = $this->fname.", ".$this->lname;

      return $name;

   }
}
jonstjohn
ok, thanks i got it.
+1  A: 

They way you have it now, if you call:

$d = new hello(); 
$d->say();

Nothing happens because say() simply returns the value... you need to echo or print that value out as follows:

$d = new hello();
echo $d->say();

But this will only output that comma you have assigned to $name. The values of $this->fname and $this->lname have not been populated so there is nothing in them to return.

But wait, you might say... I have that method attrib() in there, shouldn't that populate those variables? The answer is yes, but you have to call it. So using your example the following would get you what you want:

$d = new hello(); //instantiates the class
$d->attrib();     //calls attrib() thus assigning those values
echo $d->say();   //echos out the values returned by say()

Most importantly, however, is that fact that this isn't really the best way to do something like this. The answers provided by Paolo and jonstjohn show you the best way to handle this and Paolo's is the most flexible as you can change the values of first and last name each time.

gaoshan88