Phương pháp bảo quản Chức năng gốc Trong khi phương pháp ghi đè Để thêm chức năng mới cho một phương pháp di truyền trong khi vẫn giữ phương pháp ban đầu nguyên vẹn, sử dụng từ khoá cha mẹ | CHAPTER 3 OBJECT-ORIENTED PROGRAMMING return this- getProperty public function setProperty newval this- prop1 newval public function getProperty return this- prop1 . br class MyOtherClass extends MyClass public function construct echo A new constructor in . CLASS__ . . br public function newMethod echo From a new method in . CLASS__ . . br Create a new object newobj new MyOtherClass Output the object as a string echo newobj- newMethod Use a method from the parent class echo newobj- getProperty This changes the output in the browser to A new constructor in MyOtherClass. From a new method in MyOtherClass. I m a class property The class MyClass was destroyed. 101 CHAPTER 3 OBJECT-ORIENTED PROGRAMMING 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 php class MyClass public prop1 I m a class property public function construct echo The class CLASS__ was initiated br public function destruct echo The class CLASS__ was destroyed. br public function toString echo Using the toString method return this- getProperty public function setProperty newval this- prop1 newval public function getProperty return this- prop1 . br class MyOtherClass extends MyClass public function construct parent __construct Call the parent class s constructor echo A new constructor in . CLASS__ . . br public function newMethod echo From a new method in . CLASS__ . . br 102 CHAPTER 3 OBJECT-ORIENTED PROGRAMMING Create a new object newobj new MyOtherClass Output the object as a string echo newobj- newMethod Use a method from the parent class echo newobj- getProperty This outputs the result of both the parent constructor and the new class s constructor The class MyClass was initiated A new constructor in MyOtherClass. From a new method in MyOtherClass. I m a class property The class MyClass was destroyed. .