Edit:
*Note: I’m using PHP 5.2 for the time being, unfortunately. I can’t find a decent cheap host offering 5.3…
In PHP, self refers to the class in which the called method is defined. This means that if you don’t override a method in the child class, the keyword self will refer to the parent class, even when called from the child.
For example, this code:
<?php
class ParentClass {
const NAME = "ParentClass";
public function showName() {
echo self::NAME . "<br />\n";
}
}
class ChildClass extends ParentClass {
const NAME = "ChildClass";
public function __construct() {
echo self::NAME . "<br />\n";
}
}
$test = new ChildClass();
$test->showName();
?>
Will create this output:
ChildClass
ParentClass
What I want to do is to create a default method (e.g. showName() in the example above) which exists in a parent class with constants defining default values to use. In the child, I want to be able to override these constants (note the const in the child definition above), and have those values be used when I call the method on an instance of the child.
In short, how can I make it so that the output of the above sample would be…
ChildClass
ChildClass
…without having to duplicate the code of the parent within the child?
I believe the according syntactic salt for your case is: