Puppy
Puppy

Reputation: 147056

PHP- calling method on non-object, but it's clearly an object

class DOM extends ContentTag {
    private $body;
    private $head;
    public function Head() {
        return $head;
    }
    public function Body() {
        return $body;
    }
    public function __construct() {
        parent::__construct('html');
        Tag::Extras('xmlns="http://www.w3.org/1999/xhtml"');
        $head = new ContentTag('head');
        $body = new ContentTag('body');
        ContentTag::AddTag($head);
        ContentTag::AddTag($body);
        $head->AddTag(MakeTag('meta')->Extras('http-equiv="Content-Type" content="text/html; charset=utf-8"'));
    }
    public function Emit() {
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
        Emit();
    }
}

// top.php
$pagediv = 0;
$view = new DOM();
$view->Head()->AddTag(MakeLink('css/style.css', 'stylesheet', 'text/css'));

This code fails on the bottom line, where accessing the variable and then calling AddTag fails- even though I called AddTag on that exact variable in the constructor of DOM just fine. The code parses fine- is this some strange precedence.. something or what?

Upvotes: 1

Views: 129

Answers (1)

alex
alex

Reputation: 490657

You need to set and return $head property with $this->head.

Otherwise the $head property will be NULL.

Upvotes: 2

Related Questions