tags:

views:

37

answers:

3

I am puzzled why it won't display the name. I expect to see the output, "Jeffrey" but it doesn't. I created an object with an argument to be passed to the constructor so I expected "JEFFREY" to be displayed but it didn't. I added the echo function in constructor to see if it output jeffrey and it didn't echo either. PHP didn't show any errors. What is that I am doing wrong?

class person {

 public $name;

 function __constructor($name)
 {
  $this->name=strtoupper($name); 
  echo $name;
  }

   function displayName()
  {
   echo "<h1>$this->name</h1>";  
   }

 }

 $m = new person("jeffrey");
 $m->displayName();
+5  A: 

You have a syntax mistake: The constructor must be named __construct(), not __constructor().

Your constructor is never called, $this->name never saved.

Pekka
Now, I'm laughing. :) Thanks for pointing out the wrong function. I was looking at "constructor" on PHP documentation and that's why I typed constructor().. oh well.. :)
netrox
BTW thanks again to all of you!
netrox
+1  A: 
function __construct()

not

function __constructor()
Alex Howansky
A: 

I believe that __constructor($name) should be replaced with __construct($name). __constructor is not a standard part of a class. You can also change it to person($name), if you wish.

http://www.php.net/manual/en/language.oop5.decon.php

shmeeps
`person($name)` is old PHP style, though.
Pekka
Yes, due to deprecation it is not recommended, however it will still work if someone wanted to use it.
shmeeps