Extending class methods in PHP

I am making a custom CMS and I created my base content class as follows:

class Content { public $title; public $description; public $id; static function save() { $q = "[INSERT THE DATA TO BASE CONTENT TABLE]"; } } class Image extends Content { public $location; public $thumbnail; public function save() { // I wanted to do a Content::save() here if I // declare Contents::save() as static $q = "[INSERT THE DATA TO THE IMAGE TABLE]"; } } 

My problem is that I know that a static function cannot use $this , but I know that it needs to use Content::save() .

I want Image::save() call Content::save() , but I wanted them to be named save() and declared public , not static , because I need $this .

Will the only solution be to rename Content::save() so that I can use it in Image::save() ?

Or is there a way to extend the methods?

+6
source share
1 answer

You can use parent to get the top class. Although in the following example, you call it with parent::Save , you can still use $this in the parent class.

 <?php class A { public function Save() { echo "A save"; } } class B extends A { public function Save() { echo "B save"; parent::Save(); } } $b = new B(); $b->Save(); ?> 
+6
source

Source: https://habr.com/ru/post/896957/


All Articles