When you're using inheritance, you might want to access the base class's version of a method. For example, the set_name method looks like this in the Animal class: function set_name($text) { $this->name = $text; } If you override set_name in the derived Lion class, how can you reach the Animal class's version of this method? You just preface the name of the method with Animal:: like this: function set_name($text) { Animal::set_name($text); } You can see what this looks like in phpbasemethods.php, Example 7-5, where we're accessing the base class method Animal::set_name from the new version of this method in the derived class. Example 7-5. Accessing base class methods, phpbasemethods.php<HTML> <HEAD> <TITLE> Accessing base class methods </TITLE> </HEAD> <BODY> <CENTER> <H1> Accessing base class methods </H1> <?php class Animal { var $name; function set_name($text) { $this->name = $text; } function get_name() { return $this->name; } } class Lion extends Animal { var $name; function roar() { echo $this->name, " is roaring!<BR>"; } function set_name($text) { Animal::set_name($text); } } echo "Creating your new lion...<BR>"; $lion = new Lion; $lion->set_name("Leo"); $lion->roar(); ?> </CENTER> </BODY> </HTML> You can see the results in Figure 7-5. Figure 7-5. Overriding methods.Using :: lets you specify the name of the class where PHP should search for a method, but there's a shortcut if you just want to refer to the current class's parent classyou can use the keyword parent like this: parent::set_name($text). |