Reputation: 528
I'm attempting to learn OOP and have a few questions. I've read the first few chapters in PHP Objects, Patterns, and Practice, as well as the following posts; Nettuts+, PHP Freaks, and PHPRO.
class Element {
public $tag;
public $id;
function __construct( $tag, $id ) {
$this->tag = $tag;
$this->id = $id;
}
public function getAttributes() {
return "id='{$this->id}'";
}
}
class NormalElement extends Element {
public $text;
function __construct( $tag, $id, $text ) {
parent::__construct( $tag, $id );
$this->text = $text;
}
public function getElement() {
return "<{$this->tag}>{$this->text}</{$this->tag}>";
}
}
class VoidElement extends Element {
function __construct( $tag, $id ) {
parent::__construct( $tag, $id );
}
public function getElement() {
return "<{$this->tag} " . parent::getAttributes() . " />";
}
}
I spent a while attempting to get my code to display correctly in this post, but it keeps breaking.
Upvotes: 1
Views: 361
Reputation: 137398
2) Because php stops parsing variables embedded in quoted strings when it reaches a character invalid for use in a variable name (in this case '-'). It then assumes the - is just part of the string literal. Unless, of course, you wrap it in curly braces.
Upvotes: 2
Reputation: 76955
->
operator, you need to wrap it in curly braces so that PHP knows that you're talking about a member, not $this
itself.Upvotes: 3