Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access the super class from a child class in php

I have 3 classes with the following inheritance structure:

   <?php
     class admin {
       function __construct($module){
         echo $module;
       }
     }

     class user_admin extends admin {
       function __construct(){
         parent::__construct('user');
       }
     }

     class sales_admin extends user_admin {
       function __construct(){
         parent::__construct('sales');
       }
     }

You'll notice that the sales_admin extends the user_admin, this is a nescessary step. When I run this code,

$a = new sales_admin;

it will echo "user", because it passes the "sales" string to the user_admin which doesn't accept a constructor.

Is there a way to access the constructor of the parent above it without changing the user_admin, which I don't have control over?

like image 740
Peter G Avatar asked Jan 29 '26 13:01

Peter G


1 Answers

Just reference the class directly:

class sales_admin extends user_admin
{
    function __construct()
    {
        admin::__construct('sales');
    }
}

$a = new sales_admin; // outputs 'sales'

Since user_admin extends admin, and sales_admin extends user_admin, sales_admin will have scope of the admin constructor

like image 110
AlienWebguy Avatar answered Feb 01 '26 06:02

AlienWebguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!