-1

I'm trying my first OOP PHP, but I seem to have hit a wall. As far as i can see there is nothing wrong with the code, and i get no error message. Thanks already!

<?PHP
class President {
    public $lastname;
    public $dob;
    public $dod;
    public $firstlady;
    public $wikipage;
    public $display;
    public $party;
    public $inoffice;

    public function __construct ($name, $dob, $dod, $girl, $wiki, $pic, $party, $terms){
        $this->lastname = $name;
        $this->dob = $dob;
        $this->dod = $dod;
        $this->firstlady = $girl;
        $this->wikipage = $wiki; 
        $this->display = $pic;
        $this->party = $party;
        $this->inoffice = $terms;
    }

    public function Write(){
        return "President". $name . "was in office for" . $terms . "." . "He was born on" . $dob . "and" . $dod . "." . "He represented the" . $party . "and lived in the Whitehouse with his wife" . 
        $girl . "<br/>" . "<img src'" . $pic . "' />" . "<br />" . "<a href'" . $wiki . "'> Read more on Wikipedia</a>";
    }
}

$obama = new President();
$obama->Write('Obama', 'June First 1992', 'is still alive', 'Michelle Obama', 'http://www.google.com', 'www.google.com/', 'Democrat', 'Two Terms');
echo $obama;

?>
kingkapd
  • 69
  • 8
  • `echo $obama->Write(...)`... Also pass your values in: `new ...` – Rizier123 Jul 02 '15 at 14:40
  • 1
    @Rizier123: And use `$this->name` instead of `$name`, and -perhaps- consider [Reading the manual](http://php.net/manual/en/language.oop5.php) (those things were missing form your comment... just thought I'd complete the list of things to do) – Elias Van Ootegem Jul 02 '15 at 14:41
  • Also you aren't filling your constructor with values, instead you put them in the write function. – Daan Jul 02 '15 at 14:42
  • `new President('arguments', 'go', 'here');` THEN you do `->Write()` – Marc B Jul 02 '15 at 14:43
  • 1
    And if your PHP was properly set up for development/debugging, you SHOULD have gotten a ton of undefined variable warnings: http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – Marc B Jul 02 '15 at 14:43
  • And you should echo the return value from the Write() call, not the object itself – Mark Baker Jul 02 '15 at 14:43

1 Answers1

2

first, turn on error reporting... then..

2 problems that I can see.

Firstly, youre constructor is expecting lots of parameters, which you are not passing when you instantiate the object, but when you call the Write method, which isnt expecting anything.

Then, you dont echo $obama->Write(..) when it returns a string.

solution:

$obama = new President('Obama', 'June First 1992', 'is still alive', 'Michelle Obama', 'http://www.google.com', 'www.google.com/', 'Democrat', 'Two Terms');
echo $obama->Write();

should work assuming that your parameters are right in your construct.

edit

as stated in comments below, your write method wont be able to access any of the class vars as its only looking at the local scope. You need to change your variable calls like this:

return "President". $name

to

return "President". $this->name
DevDonkey
  • 4,835
  • 2
  • 27
  • 41