Classes can inherit the methods and properties of another class using the
extends keyword. For instance, to create a second class that extends MyClass and adds a method, you would add the following to your test file:<?phpclassMyClass{public$prop1="I'm a class property!";publicfunction__construct(){echo'The class "',__CLASS__,'" was initiated!<br />';}publicfunction__destruct(){echo'The class "',__CLASS__,'" was destroyed.<br />';}publicfunction__toString(){echo"Using the toString method: ";return$this->getProperty();}publicfunctionsetProperty($newval){$this->prop1 =$newval;}publicfunctiongetProperty(){return$this->prop1 ."<br />";}}classMyOtherClassextendsMyClass{publicfunctionnewMethod(){echo"From a new method in ".__CLASS__.".<br />";}}// Create a new object$newobj=newMyOtherClass;// Output the object as a stringecho$newobj->newMethod();// Use a method from the parent classecho$newobj->getProperty();?>
Upon reloading the test file in your browser, the following is output:Theclass"MyClass"was initiated!From anewmethod in MyOtherClass.I'm aclassproperty!Theclass"MyClass"was destroyed.
Overwriting Inherited Properties and Methods
To change the behavior of an existing property or method in the new
class, you can simply overwrite it by declaring it again in the new
class:
<?phpclassMyClass{public$prop1="I'm a class property!";publicfunction__construct(){echo'The class "',__CLASS__,'" was initiated!<br />';}publicfunction__destruct(){echo'The class "',__CLASS__,'" was destroyed.<br />';}publicfunction__toString(){echo"Using the toString method: ";return$this->getProperty();}publicfunctionsetProperty($newval){$this->prop1 =$newval;}publicfunctiongetProperty(){return$this->prop1 ."<br />";}}classMyOtherClassextendsMyClass{publicfunction__construct(){echo"A new constructor in ".__CLASS__.".<br />";}publicfunctionnewMethod(){echo"From a new method in ".__CLASS__.".<br />";}}// Create a new object$newobj=newMyOtherClass;// Output the object as a stringecho$newobj->newMethod();// Use a method from the parent classecho$newobj->getProperty();?>
This changes the output in the browser to:Anewconstructor in MyOtherClass.From anewmethod in MyOtherClass.I'm aclassproperty!Theclass"MyClass"was destroyed.
Preserving Original Method Functionality While Overwriting Methods
To add new functionality to an inherited method while keeping the original method intact, use the
parent keyword with the scope resolution operator (::):<?phpclassMyClass{public$prop1="I'm a class property!";publicfunction__construct(){echo'The class "',__CLASS__,'" was initiated!<br />';}publicfunction__destruct(){echo'The class "',__CLASS__,'" was destroyed.<br />';}publicfunction__toString(){echo"Using the toString method: ";return$this->getProperty();}publicfunctionsetProperty($newval){$this->prop1 =$newval;}publicfunctiongetProperty(){return$this->prop1 ."<br />";}}classMyOtherClassextendsMyClass{publicfunction__construct(){parent::__construct();// Call the parent class's constructorecho"A new constructor in ".__CLASS__.".<br />";}publicfunctionnewMethod(){echo"From a new method in ".__CLASS__.".<br />";}}// Create a new object$newobj=newMyOtherClass;// Output the object as a stringecho$newobj->newMethod();// Use a method from the parent classecho$newobj->getProperty();?>
This outputs the result of both the parent constructor and the new class's constructor:Theclass"MyClass"was initiated!Anewconstructor in MyOtherClass.From anewmethod in MyOtherClass.I'm aclassproperty!Theclass"MyClass"was destroyed.
Post a Comment
Silahkan anda tulis komentar di bawah ini !