Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6241525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:47:01+00:00 2026-05-24T11:47:01+00:00

I have a primary class and an extended one (database connections). Parent Classname Classica

  • 0

I have a primary class and an extended one (database connections).

Parent Classname Classica
Extended DatabaseQ

How could I call the extended class methods within parent?

This is not working:

$this->connectdb();

or this:

$this->DatabaseQ->connectdb();

Example Code:

Extended

class DatabaseQ extends Classica{

    public $dbhost;
    public $dbname;
    public $dbuser;
    public $dbpass;

    function __construct(){
        include('config.php');
        $this->dbhost = $dbhost;
        $this->dbname = $dbname;
        $this->dbuser = $dbuser;
        $this->dbpass = $dbpass;
    }

    #connect to database
    public function connectdb(){      
        $link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass);
        if (!$link) {
          die('Could not connect: ' . mysql_error());
        }else {
            //echo 'Connected Successfully to Database<br>';
        }
        @mysql_select_db($this->dbname) or die( "Unable to select database!");
    } 

    #read database
    function readdb(){        
    }    
    #update database
    private function updatedb(){        
    }
    #close database connection
    function closedb(){
        mysql_close();    
    }

}

Parent

class Classica{  

        function sample_method(){
            //connect db here
            //run some sql queries here
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:47:02+00:00Added an answer on May 24, 2026 at 11:47 am

    Judging by the contents of both of your classes there is absolutely no reason for you to make any use of the Classica class at all because you are unnecessarily spreading functional responsibility over several classes when you could have it all wrapped up inside a single class.

    I see that the only reason why you are using the “parent” class is to connect to your database and do some initial queries. Unless you plan to implement some advanced design patterns later there is absolutely no reason why you could not do this inside the DatabaseQ constructor.

    class DatabaseQ {
        public $dbhost;
        public $dbname;
        public $dbuser;
        public $dbpass;
    
        function __construct(){
            include('config.php');
            $this->dbhost = $dbhost;
            $this->dbname = $dbname;
            $this->dbuser = $dbuser;
            $this->dbpass = $dbpass;
    
            $this->connectdb(); // This is a good place to initiate your DB connection
            $this->doOtherInitStuff(); // Calling the rest of the init stuff.
        }
    
        /**
         * This is the place where you do all of your init stuff. 
         * Note the private status! The environment doesn't need to have access to your DB initialization stuff
         */
        private function doOtherInitStuff() {
            // Do init stuff
        }
    
        #connect to database
        private function connectdb(){ // Note the private scope too! Only the object itself needs to know how to connect to the db!
            $link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass);
            if (!$link) {
              die('Could not connect: ' . mysql_error());
            }else {
                //echo 'Connected Successfully to Database<br>';
            }
            @mysql_select_db($this->dbname) or die( "Unable to select database!");
        } 
    
        #read database
        function readdb(){        
        }    
        #update database
        private function updatedb(){        
        }
        #close database connection
        function closedb(){
            mysql_close();    
        }
    
    }
    

    On the other hand if you intend to create a base class which will be used later for let say different DB “drivers” (extended classes with overloaded methods) you could create an abstract class which will only contain the blueprint for all the methods which your extended (driver) classes would need to implement.

    But that’s a bit advanced story 🙂

    EDIT: If you need a class which would specifically be used for outputting stuff which DatabaseQ retrieves then create an extended class of DatabaseQ and put inside of it everything that will spit data out.

    class DatabaseQOutput extends DatabaseQ {
        public function __construct(){
            parent::__construct(); // You make sure here that the parents constructor is executed and a DB connection and initialization stuff is taken care off 
        }
    
        public function output() {
    
        }
    }
    
    $db = new DatabaseQOutput();
    $db->output();
    

    But to tell you the truth you don’t actually want any of your database specific classes to be responsible for outputting data because generally that’s not their job. Database classes should be considered as models although you are not using MVC which means their role is primarily to serve as an abstraction layer for databases and all data fetching / sending operations.

    If I were you I would create a class which is specifically tasked with outputting data which is retrieved with your database classes. That way you would create a class which is acting as a view in a way and will accept all responsibility for outputting data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with a primary key that is stored in a database.
I have built a database helper class with an open() method and extended sqlite
I have a MySQL database with 60 tables. Most of the tables have primary
Should I always have a primary key in my database tables? Let's take the
I'm creating a database table and I don't have a logical primary key assigned
I wanna create query by primary key. Supposed I have class primary key, PersonKey,
I have primary key sequence for oracle in my Castle ActiveRecord class: [PrimaryKey(PrimaryKeyType.SeqHiLo, UserId,
This one is a long one, bellow is my primary class that holds the
I have a JPA entity class with a composite primary key (uid,lid) that in
I have 3 primary keys like in the following example: @Entity class User {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.